Closed
Description
Bug Report
🔎 Search Terms
Generic function method static constructor void optional required
🕗 Version & Regression Information
- Tested up to 4.3.0-beta
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics and function signatures.
⏯ Playground Link
💻 Code
/** Standard void behavior */
function f<T>(x: void) {}
f(undefined); // OK
f(); // OK
/** Void behavior when using `extends` syntax */
function g<T>(x: T extends any ? void : void) {}
g(undefined) // OK
g(); // Expected 1 arguments, but got 0.
g<string>(); // Expected 1 arguments, but got 0.
g<number>(); // Expected 1 arguments, but got 0.
/** Class example - note that methods appear to behave as expected */
class A<T> {
constructor(x: T extends number ? void : void) {}
static f<T>(x: T extends number ? void : void) {}
f(x: T extends number ? void : void) {}
}
new A<string>(); // Error: Expected 1 arguments, but got 0.
A.f<string>(); // Error: Expected 1 arguments, but got 0
new A<string>(undefined).f(); // OK
🙁 Actual behavior
For functions, static methods, constructors, that use the extends
syntax, typescript treats void
arguments as required.
🙂 Expected behavior
Arguments that are known to be void
are always treated as optional.