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
As discussed in #5843, test1 is not supposed to typecheck. However, surprisingly, test2 does.
TypeScript infers T for a and Test for b.
I don't know whether this is intended - can I build upon that behavior?
The text was updated successfully, but these errors were encountered:
The type { new(): T } | Function is equivalent to Function because { new(): T } is assignable to Function (the same way the type Dog | Animal is equivalent to Animal). We call this "subtype reduction" and in general it's really bad to write down a type that undergoes immediate reduction (we probably should have made this an error when we made union types, but too late now).
Most of the time when people write Function the actual type they want is (...args: any[]) => any):
Thanks for your answer!
But still, the type of the expression test2(Test) infers to Test. If { new():T } | Function is actually equivalent to Function, TypeScript should have inferred any, as it does for test3(Test) in
functiontest3<T>(arg: Function): T{returnnull!;}
However, I like TypeScripts current inference behavior as it solves #5843. I need this for my remoting library that instantiates abstract classes (which need to be classes so they can be decorated with metadata).
I have this code in TS 2.1:
As discussed in #5843,
test1
is not supposed to typecheck. However, surprisingly,test2
does.TypeScript infers
T
fora
andTest
forb
.I don't know whether this is intended - can I build upon that behavior?
The text was updated successfully, but these errors were encountered: