Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(50340): typeof ... === "undefined" check on discriminated union o…
…f undefined and object type doesn't narrow correctly (#50344) * fix(50340): narrow type by discriminant in typeof * add additional test cases
- Loading branch information
1 parent
43f8ae6
commit a9797d2
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//// [narrowingTypeofUndefined.ts] | ||
declare const a: { error: { prop: string }, result: undefined } | { error: undefined, result: { prop: number } } | ||
|
||
if (typeof a.error === 'undefined') { | ||
a.result.prop; // number | ||
} | ||
else { | ||
a.error.prop; // string | ||
} | ||
|
||
if (typeof a.error !== 'undefined') { | ||
a.error.prop; // string | ||
} | ||
else { | ||
a.result.prop; // number | ||
} | ||
|
||
|
||
//// [narrowingTypeofUndefined.js] | ||
if (typeof a.error === 'undefined') { | ||
a.result.prop; // number | ||
} | ||
else { | ||
a.error.prop; // string | ||
} | ||
if (typeof a.error !== 'undefined') { | ||
a.error.prop; // string | ||
} | ||
else { | ||
a.result.prop; // number | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/baselines/reference/narrowingTypeofUndefined.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
=== tests/cases/compiler/narrowingTypeofUndefined.ts === | ||
declare const a: { error: { prop: string }, result: undefined } | { error: undefined, result: { prop: number } } | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18)) | ||
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27)) | ||
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43)) | ||
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85)) | ||
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95)) | ||
|
||
if (typeof a.error === 'undefined') { | ||
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
|
||
a.result.prop; // number | ||
>a.result.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95)) | ||
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85)) | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85)) | ||
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95)) | ||
} | ||
else { | ||
a.error.prop; // string | ||
>a.error.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27)) | ||
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27)) | ||
} | ||
|
||
if (typeof a.error !== 'undefined') { | ||
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
|
||
a.error.prop; // string | ||
>a.error.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27)) | ||
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67)) | ||
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27)) | ||
} | ||
else { | ||
a.result.prop; // number | ||
>a.result.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95)) | ||
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85)) | ||
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13)) | ||
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85)) | ||
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
=== tests/cases/compiler/narrowingTypeofUndefined.ts === | ||
declare const a: { error: { prop: string }, result: undefined } | { error: undefined, result: { prop: number } } | ||
>a : { error: { prop: string;}; result: undefined; } | { error: undefined; result: { prop: number;}; } | ||
>error : { prop: string; } | ||
>prop : string | ||
>result : undefined | ||
>error : undefined | ||
>result : { prop: number; } | ||
>prop : number | ||
|
||
if (typeof a.error === 'undefined') { | ||
>typeof a.error === 'undefined' : boolean | ||
>typeof a.error : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>a.error : { prop: string; } | ||
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; } | ||
>error : { prop: string; } | ||
>'undefined' : "undefined" | ||
|
||
a.result.prop; // number | ||
>a.result.prop : number | ||
>a.result : { prop: number; } | ||
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; } | ||
>result : { prop: number; } | ||
>prop : number | ||
} | ||
else { | ||
a.error.prop; // string | ||
>a.error.prop : string | ||
>a.error : { prop: string; } | ||
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; } | ||
>error : { prop: string; } | ||
>prop : string | ||
} | ||
|
||
if (typeof a.error !== 'undefined') { | ||
>typeof a.error !== 'undefined' : boolean | ||
>typeof a.error : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>a.error : { prop: string; } | ||
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; } | ||
>error : { prop: string; } | ||
>'undefined' : "undefined" | ||
|
||
a.error.prop; // string | ||
>a.error.prop : string | ||
>a.error : { prop: string; } | ||
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; } | ||
>error : { prop: string; } | ||
>prop : string | ||
} | ||
else { | ||
a.result.prop; // number | ||
>a.result.prop : number | ||
>a.result : { prop: number; } | ||
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; } | ||
>result : { prop: number; } | ||
>prop : number | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare const a: { error: { prop: string }, result: undefined } | { error: undefined, result: { prop: number } } | ||
|
||
if (typeof a.error === 'undefined') { | ||
a.result.prop; // number | ||
} | ||
else { | ||
a.error.prop; // string | ||
} | ||
|
||
if (typeof a.error !== 'undefined') { | ||
a.error.prop; // string | ||
} | ||
else { | ||
a.result.prop; // number | ||
} |