-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: nightly (2.1.0-dev.20161002)
Code
function second<T>(tuple: [any, T]): T
function second<T>(tuple: [any, T, any]): T
function second(tuple: any[]): any {
return tuple[1]
}
function segundo<T>(tuple: [any, T] | [any, T, any]): T {
return tuple[1]
}
const pairs: [number, string][] = [[1, "two"]]
const x: number[] = pairs.map(second)
const y: number[] = pairs.map(segundo)Expected behavior:
Both x and y are compile errors.
Actual behavior:
y fails with:
a.ts(13,7): error TS2322: Type 'string[]' is not assignable to type 'number[]'.
Type 'string' is not assignable to type 'number'.
but x does not fail, because the generic parameter of map is inferred as any rather than string, even with --noImplicitAny turned on.
On another note, I'm not sure what's happening below: either the type parameter of identity is inferred as any or it's inferred as string & number and function bivariance wreaks its magic:
function useStringToNumber(f: (s: string) => number): void {}
function identity<T>(t: T): T {
return t
}
useStringToNumber(identity)sergey-shandar and gcnew
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created