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

Conversion to number using unary plus doesn't work properly with null #21574

Closed
zaclummys opened this issue Feb 2, 2018 · 5 comments
Closed
Labels
Suggestion An idea for TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@zaclummys
Copy link

TypeScript Version: 2.8.0-dev.20180202

Code

// Test 1
String(null);

// Test 2
Number(null);

// Test 3
'' + null;

// Test 4
+ null;

Expected behavior:
All tests must be ok.

Actual behavior:
Test 4 isn't ok. Typescript says: Object is possibly 'null'.

Playground Link: https://www.typescriptlang.org/play/index.html#src=%2F%2F%20Test%201%0D%0AString(null)%3B%0D%0A%0D%0A%2F%2F%20Test%202%0D%0ANumber(null)%3B%0D%0A%0D%0A%2F%2F%20Test%203%0D%0A''%20%2B%20null%3B%0D%0A%0D%0A%2F%2F%20Test%204%0D%0A%2B%20null%3B

@mhegazy
Copy link
Contributor

mhegazy commented Feb 2, 2018

#7989 tracks the general operator interaction with nullable types.. but we chose to allow string + null|undefined to enable some debugging scenarios, see #7989 (comment)

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 2, 2018
@ghost
Copy link

ghost commented Feb 2, 2018

Related: #20131 -- although that case is requesting the compiler to be more strict.

@zaclummys
Copy link
Author

zaclummys commented Feb 2, 2018

@mhegazy,
if TypeScript doesn't allow + null, why it's allowed to use Number(null)?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 2, 2018

I think i miss linked this issue to #7989. we should think about this one more time..

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Duplicate An existing issue was already created labels Feb 2, 2018
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed In Discussion Not yet reached consensus labels Feb 5, 2018
@RyanCavanaugh
Copy link
Member

Related #20754

We like the behavior as-is, inconsistency notwithstanding

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants