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
declarefunctionhead(str: string): string;declarefunctionhead<T>(arr: T[]): T;constnumberLists: number[][]=[[1,2],[3,4]];// This makes senseconstnumberHeads: number[]=numberLists.map(head);// This shouldn't compileconststringHeads: string[]=numberLists.map(head);// This shouldn't compileconstbooleanHeads: boolean[]=numberLists.map(head);// This shouldn't compileconstnullHeads: null[]=numberLists.map(head);
Expected behavior:
Described above in comments
Actual behavior:
All the cases compile, because the return type of all numberLists.map calls here is any[]. Reversing the order of the overloaded function declarations results in this:
cases 3, 4 fail to compile (correct)
case 1 fails to compile (incorrect)
case 2 compiles (incorrect)
There are quite a lot of open issues involving function overloading, but I couldn't find one about forced any-typed return values, which are a silent killer in a codebase because even noImplicitAny doesn't catch these. I wouldn't mind if all the use sites required extra types, but in this case I don't seem to have a way of avoiding accidental loss of type safety.
The text was updated successfully, but these errors were encountered:
TypeScript Version: 2.2.1 + nightly (2.3.0-dev.20170301)
Code
Expected behavior:
Described above in comments
Actual behavior:
All the cases compile, because the return type of all
numberLists.map
calls here isany[]
. Reversing the order of the overloaded function declarations results in this:There are quite a lot of open issues involving function overloading, but I couldn't find one about forced any-typed return values, which are a silent killer in a codebase because even
noImplicitAny
doesn't catch these. I wouldn't mind if all the use sites required extra types, but in this case I don't seem to have a way of avoiding accidental loss of type safety.The text was updated successfully, but these errors were encountered: