You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classA<T>{constructor(a: T){}}classB{constructor(a: void){}}consta=newA<void>();// error ! => variable 'a' should be optional !constb=newB();
Expected behavior:
It's pretty hard to understand why with a template function having a 'void' type (in class A), the behaviour is not the same as the class B.
Allowing a developper to put 'void' as template and providing 0 argument is sometimes required.
The following 'solution' is not acceptable, because the variable a would always be optional where I want to have it optional only if T is void.
classA<T>{constructor(a?: T){}}
Actual behavior:
class A diverge from B even if the typing is conceptualy the same.
typeArgs<T>=Textendsvoid ? [T?] : [T];functionfoo<T>(...args: Args<T>): void;functionfoo<T>(arg: T){// separate declaration from implementation, so the implementation doesn't need to use rest params}foo<void>();// works!foo<number>();// fails as expectedfoo<number>(3);// works!
TypeScript Version: 3.4.5, 3.5.0-rc and next
Search Terms:
void, function, argument, template
Code
Expected behavior:
It's pretty hard to understand why with a template function having a 'void' type (in class A), the behaviour is not the same as the class B.
Allowing a developper to put 'void' as template and providing 0 argument is sometimes required.
The following 'solution' is not acceptable, because the variable
a
would always be optional where I want to have it optional only if T is void.Actual behavior:
class A diverge from B even if the typing is conceptualy the same.
Playground Link:
https://www.typescriptlang.org/play/#src=class%20A%3CT%3E%20%7B%0D%0A%20%20constructor(a%3A%20T)%20%7B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20B%20%7B%0D%0A%20%20constructor(a%3A%20void)%20%7B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0A%0D%0Aconst%20a%20%3D%20new%20A%3Cvoid%3E()%3B%20%2F%2F%20error%20!%0D%0Aconst%20b%20%3D%20new%20B()%3B
More examples:
The text was updated successfully, but these errors were encountered: