Skip to content

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

Closed

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

Activity

  1. changed the title [-]Properties with string keys and already declared type are getting their type reduced when using square bracket assignment.[/-] [+]Properties with string keys and already declared type are getting their type narrowed when using square bracket assignment.[/+] on Sep 28, 2018
  2. added a commit that references this issue on Oct 5, 2018
    f5190d1
  3. added a commit that references this issue on Oct 5, 2018
    c080324
  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions