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
typet1={a: number;b: string;}|{a: string;};// no error. Invalid assignment is possibleconstdemo1: t1={a: '123',b: 'asd',};typet2={a: boolean;b: string;}|{a: string;};// error Error: TS2322: Type '{ a: string; b: string; }' is not assignable to type 't2'. Object literal may only specify known properties, and 'b' does not exist in type '{ a: string; }'.constdemo2: t2={a: '123',b: 'asd',};
Output
"use strict";// no errorconstdemo1={a: '123',b: 'asd',};// errorconstdemo2={a: '123',b: 'asd',};
The text was updated successfully, but these errors were encountered:
evxn
changed the title
Union type narrowing assignment fails for boolean and works for other primitives
Union type narrowing during assignment fails for boolean and works for other primitives
Dec 26, 2019
evxn
changed the title
Union type narrowing during assignment fails for boolean and works for other primitives
Union type checking during assignment fails for boolean and works for other primitives
Dec 26, 2019
evxn
changed the title
Union type checking during assignment fails for boolean and works for other primitives
Union type checking during assignment fails for boolean and passes for other primitives
Dec 26, 2019
TypeScript Version: 3.7.2
Search Terms:
number string union
Expected behavior:
Compiler should be giving an error on assigning
demo1
, like it does withdemo2
.Actual behavior:
Doesn't seem to properly discriminate between the
string
and other types that are conceptually unbound:number
/RegExp
etc.Related Issues:
#20863
#30506
#9254
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: