Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect function parameter not detected #13248

Closed
TeamworkGuy2 opened this issue Jan 2, 2017 · 3 comments
Closed

Incorrect function parameter not detected #13248

TeamworkGuy2 opened this issue Jan 2, 2017 · 3 comments
Labels
Fixed A PR has been merged for this issue

Comments

@TeamworkGuy2
Copy link

TypeScript Version: (maybe 2.1.x - typescriptlang.org playground version as of 2017-1-2)

Code
typescriptlang.org playground

[1, 2, 3].map(mapper); // fine???
[1, 2, 3].map((obj, idx) => mapper(obj, idx)); // <= error

type Tag = number & { _tagBrand: any; };

function mapper<E>(obj: E, type: Tag) {
    return obj;
}

Expected behavior:
The first map() call on line 1 should throw the same compile error as the second call, since the second parameter of the mapper function does not match the second parameter of the function expected by Array.map()

Actual behavior:
No compile error on the first map() call when it should.

Found while working on nominal types based on basarat's git book 'Nominal Typings' section.

Reproduced in VS 2015 update 3 TypeScript 2.1.4 and the typescriptlang.org playground.

@akarzazi
Copy link

akarzazi commented Jan 2, 2017

Function parameters are Bivariants
Here is a sample

let fct: (x: number) => void = (a: Tag) => console.log(a); // ok 
let fft: (x: Tag) => void = (a: number) => console.log(a); // ok also

Read more :
http://www.typescriptlang.org/docs/handbook/type-compatibility.html
-> Function Parameter Bivariance

@zpdDG4gta8XKpMCd
Copy link

function parameters are bivariants

and this is a shame

@TeamworkGuy2
Copy link
Author

Thanks @akarzazi, it definitely seems like a strange design, but TypeScript is pretty awesome so I'm not complaining :)

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Sep 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants