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

Invalid function assignment is not detected #18337

Closed
VictorNicollet opened this issue Sep 8, 2017 · 2 comments
Closed

Invalid function assignment is not detected #18337

VictorNicollet opened this issue Sep 8, 2017 · 2 comments
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue

Comments

@VictorNicollet
Copy link

TypeScript Version: 2.5.0

Code

const a: (d: string) => number = (d) => d.length;
const b: (d?: string) => number = a;
b();

Expected behavior:
When compiled with --strict, the assignment of b should cause a type error along the lines of:

string | undefined is not assignable to type string.

Actual behavior:
The code compiles with --strict, then throws an exception at runtime on the expression d.length.

@gcnew
Copy link
Contributor

gcnew commented Sep 8, 2017

Unfortunately that's expected behaviour: FAQ - Why are function parameters bivariant.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 8, 2017
@VictorNicollet
Copy link
Author

Thank you. This is disappointing, but I understand the reasons.

@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 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants