From dfd75afd9b7c48614699be2008b089c39af62d06 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 25 Jun 2018 17:54:43 -0700 Subject: [PATCH 1/3] For typeof narrow all union members prior to filtering --- src/compiler/checker.ts | 12 +++++++----- .../TypeScript-Node-Starter/TypeScript-Node-Starter | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 27e1b8117f441..06b30d2065127 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14184,7 +14184,12 @@ namespace ts { if (type.flags & TypeFlags.Any && literal.text === "function") { return type; } - if (assumeTrue && !(type.flags & TypeFlags.Union)) { + const facts = assumeTrue ? + typeofEQFacts.get(literal.text) || TypeFacts.TypeofEQHostObject : + typeofNEFacts.get(literal.text) || TypeFacts.TypeofNEHostObject; + return getTypeWithFacts(assumeTrue ? mapType(type, narrowTypeForTypeof) : type, facts); + + function narrowTypeForTypeof(type: Type) { // We narrow a non-union type to an exact primitive type if the non-union type // is a supertype of that primitive type. For example, type 'any' can be narrowed // to one of the primitive types. @@ -14200,11 +14205,8 @@ namespace ts { } } } + return type; } - const facts = assumeTrue ? - typeofEQFacts.get(literal.text) || TypeFacts.TypeofEQHostObject : - typeofNEFacts.get(literal.text) || TypeFacts.TypeofNEHostObject; - return getTypeWithFacts(type, facts); } function narrowTypeBySwitchOnDiscriminant(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number) { diff --git a/tests/cases/user/TypeScript-Node-Starter/TypeScript-Node-Starter b/tests/cases/user/TypeScript-Node-Starter/TypeScript-Node-Starter index 6b9706810b55a..40bdb4eadabc9 160000 --- a/tests/cases/user/TypeScript-Node-Starter/TypeScript-Node-Starter +++ b/tests/cases/user/TypeScript-Node-Starter/TypeScript-Node-Starter @@ -1 +1 @@ -Subproject commit 6b9706810b55af326a93b9aa59cb17815a30bb32 +Subproject commit 40bdb4eadabc9fbed7d83e3f26817a931c0763b6 From 16ddc4a82aa0f7b05f412f07dd400407eae0e40b Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Jun 2018 13:05:28 -0700 Subject: [PATCH 2/3] Revise narrowTypeByTypeof to both narrow unions and applicable union members --- src/compiler/checker.ts | 2 +- .../reference/controlFlowIfStatement.types | 2 +- .../reference/recursiveTypeRelations.types | 4 +-- ...ypeGuardOfFormTypeOfPrimitiveSubtype.types | 12 +++---- .../reference/typeGuardTypeOfUndefined.types | 36 +++++++++---------- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4df8362a01acd..aa179074b7c31 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14330,7 +14330,7 @@ namespace ts { const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text); if (targetType) { if (isTypeSubtypeOf(targetType, type)) { - return targetType; + return isTypeAny(type) ? targetType : getIntersectionType([type, targetType]); // Intersection to handle `string` being a subtype of `keyof T` } if (type.flags & TypeFlags.Instantiable) { const constraint = getBaseConstraintOfType(type) || anyType; diff --git a/tests/baselines/reference/controlFlowIfStatement.types b/tests/baselines/reference/controlFlowIfStatement.types index 7c745735c9efd..534885666093a 100644 --- a/tests/baselines/reference/controlFlowIfStatement.types +++ b/tests/baselines/reference/controlFlowIfStatement.types @@ -108,7 +108,7 @@ function c(data: string | T): T { >JSON.parse : (text: string, reviver?: (key: any, value: any) => any) => any >JSON : JSON >parse : (text: string, reviver?: (key: any, value: any) => any) => any ->data : string +>data : string | (T & string) } else { return data; diff --git a/tests/baselines/reference/recursiveTypeRelations.types b/tests/baselines/reference/recursiveTypeRelations.types index c7a42b2f309bc..2e5a2935b2cc3 100644 --- a/tests/baselines/reference/recursiveTypeRelations.types +++ b/tests/baselines/reference/recursiveTypeRelations.types @@ -90,9 +90,9 @@ export function css(styles: S, ...classNam >"string" : "string" return styles[arg]; ->styles[arg] : S[keyof S] +>styles[arg] : S[keyof S & string] >styles : S ->arg : keyof S +>arg : keyof S & string } if (typeof arg == "object") { >typeof arg == "object" : boolean diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types index 4787c07d75807..6510b73d44d53 100644 --- a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types +++ b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types @@ -14,7 +14,7 @@ if (typeof a === "number") { let c: number = a; >c : number ->a : number +>a : number & {} } if (typeof a === "string") { >typeof a === "string" : boolean @@ -24,7 +24,7 @@ if (typeof a === "string") { let c: string = a; >c : string ->a : string +>a : string & {} } if (typeof a === "boolean") { >typeof a === "boolean" : boolean @@ -34,7 +34,7 @@ if (typeof a === "boolean") { let c: boolean = a; >c : boolean ->a : boolean +>a : (false & {}) | (true & {}) } if (typeof b === "number") { @@ -45,7 +45,7 @@ if (typeof b === "number") { let c: number = b; >c : number ->b : number +>b : { toString(): string; } & number } if (typeof b === "string") { >typeof b === "string" : boolean @@ -55,7 +55,7 @@ if (typeof b === "string") { let c: string = b; >c : string ->b : string +>b : { toString(): string; } & string } if (typeof b === "boolean") { >typeof b === "boolean" : boolean @@ -65,6 +65,6 @@ if (typeof b === "boolean") { let c: boolean = b; >c : boolean ->b : boolean +>b : ({ toString(): string; } & false) | ({ toString(): string; } & true) } diff --git a/tests/baselines/reference/typeGuardTypeOfUndefined.types b/tests/baselines/reference/typeGuardTypeOfUndefined.types index a28dab044907a..00777ac0eae0e 100644 --- a/tests/baselines/reference/typeGuardTypeOfUndefined.types +++ b/tests/baselines/reference/typeGuardTypeOfUndefined.types @@ -134,7 +134,7 @@ function test5(a: boolean | void) { } else { a; ->a : boolean | void +>a : undefined } } @@ -151,15 +151,15 @@ function test6(a: boolean | void) { if (typeof a === "boolean") { >typeof a === "boolean" : boolean >typeof a : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" ->a : boolean | void +>a : undefined >"boolean" : "boolean" a; ->a : boolean +>a : never } else { a; ->a : void +>a : undefined } } else { @@ -184,7 +184,7 @@ function test7(a: boolean | void) { >"boolean" : "boolean" a; ->a : boolean | void +>a : boolean } else { a; @@ -212,7 +212,7 @@ function test8(a: boolean | void) { } else { a; ->a : boolean | void +>a : undefined } } @@ -242,7 +242,7 @@ function test9(a: boolean | number) { } else { a; ->a : number | boolean +>a : undefined } } @@ -259,15 +259,15 @@ function test10(a: boolean | number) { if (typeof a === "boolean") { >typeof a === "boolean" : boolean >typeof a : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" ->a : number | boolean +>a : undefined >"boolean" : "boolean" a; ->a : boolean +>a : never } else { a; ->a : number +>a : undefined } } else { @@ -292,7 +292,7 @@ function test11(a: boolean | number) { >"boolean" : "boolean" a; ->a : number | boolean +>a : boolean } else { a; @@ -320,7 +320,7 @@ function test12(a: boolean | number) { } else { a; ->a : number | boolean +>a : number } } @@ -350,7 +350,7 @@ function test13(a: boolean | number | void) { } else { a; ->a : number | boolean | void +>a : undefined } } @@ -367,15 +367,15 @@ function test14(a: boolean | number | void) { if (typeof a === "boolean") { >typeof a === "boolean" : boolean >typeof a : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" ->a : number | boolean | void +>a : undefined >"boolean" : "boolean" a; ->a : boolean +>a : never } else { a; ->a : number | void +>a : undefined } } else { @@ -400,7 +400,7 @@ function test15(a: boolean | number | void) { >"boolean" : "boolean" a; ->a : number | boolean | void +>a : boolean } else { a; @@ -428,7 +428,7 @@ function test16(a: boolean | number | void) { } else { a; ->a : number | boolean | void +>a : number } } From 5772ab69fa72b499c0f650a47d8d68cfb0654631 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Jun 2018 13:10:34 -0700 Subject: [PATCH 3/3] Add repros from issue --- .../reference/strictTypeofUnionNarrowing.js | 32 +++++++++ .../strictTypeofUnionNarrowing.symbols | 47 ++++++++++++ .../strictTypeofUnionNarrowing.types | 71 +++++++++++++++++++ .../compiler/strictTypeofUnionNarrowing.ts | 16 +++++ 4 files changed, 166 insertions(+) create mode 100644 tests/baselines/reference/strictTypeofUnionNarrowing.js create mode 100644 tests/baselines/reference/strictTypeofUnionNarrowing.symbols create mode 100644 tests/baselines/reference/strictTypeofUnionNarrowing.types create mode 100644 tests/cases/compiler/strictTypeofUnionNarrowing.ts diff --git a/tests/baselines/reference/strictTypeofUnionNarrowing.js b/tests/baselines/reference/strictTypeofUnionNarrowing.js new file mode 100644 index 0000000000000..93b9c7d433013 --- /dev/null +++ b/tests/baselines/reference/strictTypeofUnionNarrowing.js @@ -0,0 +1,32 @@ +//// [strictTypeofUnionNarrowing.ts] +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() : ""; +} + + +//// [strictTypeofUnionNarrowing.js] +"use strict"; +function stringify1(anything) { + return typeof anything === "string" ? anything.toUpperCase() : ""; +} +function stringify2(anything) { + return typeof anything === "string" ? anything.toUpperCase() : ""; +} +function stringify3(anything) { + return typeof anything === "string" ? anything.toUpperCase() : ""; +} +function stringify4(anything) { + return typeof anything === "string" ? anything.toUpperCase() : ""; +} diff --git a/tests/baselines/reference/strictTypeofUnionNarrowing.symbols b/tests/baselines/reference/strictTypeofUnionNarrowing.symbols new file mode 100644 index 0000000000000..83a602eeaf1d4 --- /dev/null +++ b/tests/baselines/reference/strictTypeofUnionNarrowing.symbols @@ -0,0 +1,47 @@ +=== tests/cases/compiler/strictTypeofUnionNarrowing.ts === +function stringify1(anything: { toString(): string } | undefined): string { +>stringify1 : Symbol(stringify1, Decl(strictTypeofUnionNarrowing.ts, 0, 0)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 0, 20)) +>toString : Symbol(toString, Decl(strictTypeofUnionNarrowing.ts, 0, 31)) + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 0, 20)) +>anything.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 0, 20)) +>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +} + +function stringify2(anything: {} | undefined): string { +>stringify2 : Symbol(stringify2, Decl(strictTypeofUnionNarrowing.ts, 2, 1)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 4, 20)) + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 4, 20)) +>anything.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 4, 20)) +>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +} + +function stringify3(anything: unknown | undefined): string { // should simplify to just `unknown` which should narrow fine +>stringify3 : Symbol(stringify3, Decl(strictTypeofUnionNarrowing.ts, 6, 1)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 8, 20)) + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 8, 20)) +>anything.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 8, 20)) +>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +} + +function stringify4(anything: { toString?(): string } | undefined): string { +>stringify4 : Symbol(stringify4, Decl(strictTypeofUnionNarrowing.ts, 10, 1)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 12, 20)) +>toString : Symbol(toString, Decl(strictTypeofUnionNarrowing.ts, 12, 31)) + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 12, 20)) +>anything.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +>anything : Symbol(anything, Decl(strictTypeofUnionNarrowing.ts, 12, 20)) +>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/strictTypeofUnionNarrowing.types b/tests/baselines/reference/strictTypeofUnionNarrowing.types new file mode 100644 index 0000000000000..3039ece15d68b --- /dev/null +++ b/tests/baselines/reference/strictTypeofUnionNarrowing.types @@ -0,0 +1,71 @@ +=== tests/cases/compiler/strictTypeofUnionNarrowing.ts === +function stringify1(anything: { toString(): string } | undefined): string { +>stringify1 : (anything: { toString(): string; } | undefined) => string +>anything : { toString(): string; } | undefined +>toString : () => string + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>typeof anything === "string" ? anything.toUpperCase() : "" : string +>typeof anything === "string" : boolean +>typeof anything : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" +>anything : { toString(): string; } | undefined +>"string" : "string" +>anything.toUpperCase() : string +>anything.toUpperCase : () => string +>anything : { toString(): string; } & string +>toUpperCase : () => string +>"" : "" +} + +function stringify2(anything: {} | undefined): string { +>stringify2 : (anything: {} | undefined) => string +>anything : {} | undefined + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>typeof anything === "string" ? anything.toUpperCase() : "" : string +>typeof anything === "string" : boolean +>typeof anything : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" +>anything : {} | undefined +>"string" : "string" +>anything.toUpperCase() : string +>anything.toUpperCase : () => string +>anything : string & {} +>toUpperCase : () => string +>"" : "" +} + +function stringify3(anything: unknown | undefined): string { // should simplify to just `unknown` which should narrow fine +>stringify3 : (anything: unknown) => string +>anything : unknown + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>typeof anything === "string" ? anything.toUpperCase() : "" : string +>typeof anything === "string" : boolean +>typeof anything : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" +>anything : unknown +>"string" : "string" +>anything.toUpperCase() : string +>anything.toUpperCase : () => string +>anything : string +>toUpperCase : () => string +>"" : "" +} + +function stringify4(anything: { toString?(): string } | undefined): string { +>stringify4 : (anything: {} | undefined) => string +>anything : {} | undefined +>toString : (() => string) | undefined + + return typeof anything === "string" ? anything.toUpperCase() : ""; +>typeof anything === "string" ? anything.toUpperCase() : "" : string +>typeof anything === "string" : boolean +>typeof anything : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" +>anything : {} | undefined +>"string" : "string" +>anything.toUpperCase() : string +>anything.toUpperCase : () => string +>anything : {} & string +>toUpperCase : () => string +>"" : "" +} + diff --git a/tests/cases/compiler/strictTypeofUnionNarrowing.ts b/tests/cases/compiler/strictTypeofUnionNarrowing.ts new file mode 100644 index 0000000000000..f2bddf3f939e1 --- /dev/null +++ b/tests/cases/compiler/strictTypeofUnionNarrowing.ts @@ -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() : ""; +}