You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Properties with string keys and already declared type are getting their type narrowed when using square bracket assignment. #27412
letfoo: {bar: boolean}={bar: true};foo.bar=true;// OKfoo.bar=false;// OKfoo['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
interfaceIColor{r: number;g: number;b: number;}interfaceITheme{primaryColor?: IColor;"primary-color"?: IColor;}constred: IColor={r: 1,g: 0,b: 0};functionfillInRedColor(theme: ITheme){if(!theme.primaryColor){theme.primaryColor=red;}if(!theme["primary-color"]){theme["primary-color"]=red;// Type 'IColor' is not assignable to type 'undefined'}}
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
Activity
mikea commentedon Sep 28, 2018
Another example of the code it breaks:
http://www.typescriptlang.org/play/index.html#src=interface%20IColor%20%7B%0D%0A%20%20r%3A%20number%3B%0D%0A%20%20g%3A%20number%3B%0D%0A%20%20b%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20ITheme%20%7B%0D%0A%20%20primaryColor%3F%3A%20IColor%3B%20%20%0D%0A%20%20%22primary-color%22%3F%3A%20IColor%3B%0D%0A%7D%0D%0A%0D%0Aconst%20red%3A%20IColor%20%3D%20%7B%20r%3A%201%2C%20g%3A%200%2C%20b%3A%200%20%7D%3B%0D%0A%0D%0Afunction%20fillInRedColor(theme%3A%20ITheme)%20%7B%0D%0A%20%20if%20(!theme.primaryColor)%20%7B%0D%0A%20%20%20%20theme.primaryColor%20%3D%20red%3B%20%20%0D%0A%20%20%7D%0D%0A%20%20if%20(!theme%5B%22primary-color%22%5D)%20%7B%0D%0A%20%20%20%20theme%5B%22primary-color%22%5D%20%3D%20red%3B%20%20%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0A
[-]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.[/+]Elt access assignment uses declared, not narrowed type
Elt access assignment uses declared, not narrowed type (#27574)