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

strictNullChecks and arithmetic expressions #12795

Closed
bobappleyard opened this issue Dec 9, 2016 · 3 comments
Closed

strictNullChecks and arithmetic expressions #12795

bobappleyard opened this issue Dec 9, 2016 · 3 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@bobappleyard
Copy link

TypeScript Version: 2.1.4

With strictNullChecks on:

Code

    let y = undefined + 1;

Expected behavior:

An error telling me this is the wrong thing to do.

Actual behavior:

Compiles fine, y is a number.

This does the same thing for all the arithmetic operators.

@bobappleyard
Copy link
Author

bobappleyard commented Dec 9, 2016

    let y = undefined + "";

Leads to y being a string.

Tried with {} and I get Operator '+' cannot be applied to types '{}' and '{}'

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 9, 2016
@RyanCavanaugh
Copy link
Member

@ahejlsberg not spec'd this way but I think the arithmatic operators should disallow null / undefined in strictNullChecks. Thoughts?

Adding to a string is always allowed since people use this for concatenation of arbitrary values, especially for debugging code.

@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 14, 2016
@ghost
Copy link

ghost commented Dec 20, 2016

undefined + 1 evaluates as NaN. Maybe y should be typed as the literal NaN value. NaN is almost as troublesome as null in places, and harder to deal with (NaN !== NaN, typeof NaN === 'number'). It's one of those cases of the compiler being technically correct but not very useful. Perhaps we could consider treating it specially as well.

value+"" is often used in place of String(value) and so this seems expected.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants