Skip to content

any inferred as type parameter even with --noImplicitAny #11311

Closed
@ghost

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions