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

compiler option "strictNullChecks" working unexpected for Union Type #12638

Closed
YuJianrong opened this issue Dec 3, 2016 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@YuJianrong
Copy link

TypeScript Version: 2.0.10 / nightly (2.2.0-dev.201xxxxx)

Code

let x: {[v: string]: number} | {y?: number} = {a : null};
// Both the following assigns raise compile error
// let x: {[v: string]: number} = {a : null};
// let x: {y?: number} = {a : null};

Expected behavior:
Compile error raised when "strictNullChecks" is set to true.

Actual behavior:
No compile error. Please note compile error will be raised when union type is not used.

@aluanhaddad
Copy link
Contributor

That seems correct. One of the constituents of the union is {} which is a valid assignment target.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 5, 2016
@YuJianrong
Copy link
Author

YuJianrong commented Dec 8, 2016

@aluanhaddad , but the following code is not working:

let x: {y?: number} = {a : null};

{y?:number} should be same as {}, but I get the following compile error:

message: 'Type '{ a: null; }' is not assignable to type '{ y?: number | undefined; }'.
  Object literal may only specify known properties, and 'a' does not exist in type '{ y?: number | undefined; }'.'
source: 'ts'

@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2016

seems to be the same request in #12745

@mhegazy mhegazy added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 15, 2016
@mhegazy mhegazy closed this as completed Dec 29, 2016
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants