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
typeV={a: {c: string,},b: number,}|{b: number,}constv: V={a: undefined,b: 1}if('a'inv){console.log(v.a.c);// In runtime there would be an error of accessing undefined}
π Actual behavior
No error is found
π Expected behavior
I know TS is not built to be sound, and it is more a static analytic tool for JavaScript. But is this behavior expected?
In this case, 'a' in v does not indicate v.a is { a: {.c: string } }, because v could be anything containing b: number, but we don't make any assumptions about a, since as long as b: number exists then v is V.
I also find if we initialize v as
const v: V = {
a: 0,
b: 1
}
It returns with an error Type 'number' is not assignable to type '{ c: string; }'., even though, it is a subtype of { b: number }.
So, it means "only undefined can be assigned to a"?
It seems a: undefined is a special case for object literals and union types, but it this documented anywhere?
Thanks in advance.
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
π Version & Regression Information
union types
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
No error is found
π Expected behavior
I know TS is not built to be sound, and it is more a static analytic tool for JavaScript. But is this behavior expected?
In this case,
'a' in v
does not indicatev.a
is{ a: {.c: string } }
, becausev
could be anything containingb: number
, but we don't make any assumptions abouta
, since as long asb: number
exists thenv is V
.I also find if we initialize
v
asIt returns with an error
Type 'number' is not assignable to type '{ c: string; }'.
, even though, it is a subtype of{ b: number }
.So, it means "only undefined can be assigned to
a
"?It seems
a: undefined
is a special case for object literals and union types, but it this documented anywhere?Thanks in advance.
The text was updated successfully, but these errors were encountered: