Skip to content

Broken behaviour with object property shortcuts and types union #21599

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

Closed
dfilatov opened this issue Feb 3, 2018 · 3 comments
Closed

Broken behaviour with object property shortcuts and types union #21599

dfilatov opened this issue Feb 3, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@dfilatov
Copy link

dfilatov commented Feb 3, 2018

TypeScript Version: 2.7.1

Code

type WithLoading = { loading: true; } | { loading: false; };

const loading = true;

const a: WithLoading = { loading };

Expected behavior:
No errors. And TypeScript 2.6.2 properly handles this case.
If I change the last line to const a: WithLoading = { loading: loading }; then there won't be any error, so it looks definitely like a bug.

Actual behavior:
Types of property 'loading' are incompatible. Type 'boolean' is not assignable to type 'false'.

Playground Link:
https://www.typescriptlang.org/play/index.html#src=type%20WithLoading%20%3D%20%7B%20loading%3A%20true%3B%20%7D%20%7C%20%7B%20loading%3A%20false%3B%20%7D%3B%0A%0Aconst%20loading%20%3D%20true%3B%0A%0Aconst%20a%3A%20WithLoading%20%3D%20%7B%20loading%20%7D%3B

@dfilatov dfilatov changed the title Wrong type for object Wrong type for union Feb 3, 2018
@dfilatov dfilatov changed the title Wrong type for union Broken behaviour with object property shortcuts and types union Feb 3, 2018
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Feb 4, 2018
@HyphnKnight
Copy link

HyphnKnight commented Feb 5, 2018

It should be noted this applies to tuples as well as objects

type A = [true,string,string];
type B = [false,string]
const val:A|B = [false,'test'];

https://www.typescriptlang.org/play/index.html#src=type%20A%20%3D%20%5Btrue%2Cstring%2Cstring%5D%3B%0Atype%20B%20%3D%20%5Bfalse%2Cstring%5D%0Aconst%20val%3AA%7CB%20%3D%20%5Bfalse%2C'test'%5D%3B

@mhegazy
Copy link
Contributor

mhegazy commented Feb 5, 2018

Duplicate of #21436. should be fixed in typescript@next.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Feb 5, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants