TypeScript/tests/cases/compiler/strictTypeofUnionNarrowing.ts
Wesley Wigham d8f736d319
Change typeof narrowing to narrow selected union members (#25243)
* For typeof narrow all union members prior to filtering

* Revise narrowTypeByTypeof to both narrow unions and applicable union members

* Add repros from issue
2018-09-06 00:41:09 -07:00

17 lines
660 B
TypeScript

// @strict: true
function stringify1(anything: { toString(): string } | undefined): string {
return typeof anything === "string" ? anything.toUpperCase() : "";
}
function stringify2(anything: {} | undefined): string {
return typeof anything === "string" ? anything.toUpperCase() : "";
}
function stringify3(anything: unknown | undefined): string { // should simplify to just `unknown` which should narrow fine
return typeof anything === "string" ? anything.toUpperCase() : "";
}
function stringify4(anything: { toString?(): string } | undefined): string {
return typeof anything === "string" ? anything.toUpperCase() : "";
}