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
This commit is contained in:
Wesley Wigham
2018-09-06 00:41:09 -07:00
committed by GitHub
parent c62920ac81
commit d8f736d319
9 changed files with 218 additions and 48 deletions

View File

@@ -0,0 +1,16 @@
// @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() : "";
}