Skip to content

Properties with string keys and already declared type are getting their type narrowed when using square bracket assignment. #27412

Closed
@lazytype

Description

@lazytype

TypeScript Version: 3.1.1

Search Terms: string property, property assign

Code

let foo: { bar: boolean } = { bar: true };

foo.bar = true; // OK
foo.bar = false; // OK

foo['bar'] = true; //  error TS2322: Type 'true' is not assignable to type 'false'.
foo['bar'] = false; // error TS2322: Type 'false' is not assignable to type 'true'.

Expected behavior:
No type errors
Actual behavior:
Type errors when using square backet assignment

Playground Link: https://www.typescriptlang.org/play/#src=let%20foo%3A%20%7B%20bar%3A%20boolean%20%7D%20%3D%20%7B%20bar%3A%20true%20%7D%3B%0A%0Afoo.bar%20%3D%20true%3B%0Afoo.bar%20%3D%20false%3B%0A%0Afoo%5B'bar'%5D%20%3D%20true%3B%0Afoo%5B'bar'%5D%20%3D%20false%3B

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions