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

Computed properties do not type check when they could #18596

Closed
leoasis opened this issue Sep 20, 2017 · 2 comments
Closed

Computed properties do not type check when they could #18596

leoasis opened this issue Sep 20, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@leoasis
Copy link

leoasis commented Sep 20, 2017

TypeScript Version: 2.4.2

Code

type A = { a: number, b: string };

const a: A = { a: 1, b: 'foo' };

const b: A = { ...a, b: 123 }; // doesn't type check, b should be string (correct)

const propB: 'b' = 'b';
const c: A = { ...a, [propB]: 123 }; // type checks even though i'm setting b to a number (incorrect)

Expected behavior:

Last statement should be invalid, since propB is of type 'b', which should be enough information for TS to know that i'm setting the b property.

Actual behavior:

Last statement is valid for TS. This also happens for union types of string or number literals. When the types for the values in those keys are the same, and attempting to set a value using a computed property, TS allows invalid values.

@ghost
Copy link

ghost commented Sep 20, 2017

Looks like this was fixed by #18317. In typescript@next your last line is an error.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 21, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 5, 2017

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

@mhegazy mhegazy closed this as completed Oct 5, 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
Projects
None yet
Development

No branches or pull requests

2 participants