Skip to content

Computed properties do not type check when they could #18596

@leoasis

Description

@leoasis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions