Skip to content

Change typeof narrowing to narrow selected union members #25243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14960,30 +14960,34 @@ namespace ts {
if (type.flags & TypeFlags.Any && literal.text === "function") {
return type;
}
if (assumeTrue && !(type.flags & TypeFlags.Union)) {
if (type.flags & TypeFlags.Unknown && literal.text === "object") {
return getUnionType([nonPrimitiveType, nullType]);
}
// 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.
const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text);
if (targetType) {
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
if (type.flags & TypeFlags.Instantiable) {
const constraint = getBaseConstraintOfType(type) || anyType;
if (isTypeSubtypeOf(targetType, constraint)) {
return getIntersectionType([type, targetType]);
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) {
if (assumeTrue && !(type.flags & TypeFlags.Union)) {
if (type.flags & TypeFlags.Unknown && literal.text === "object") {
return getUnionType([nonPrimitiveType, nullType]);
}
// 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.
const targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text);
if (targetType) {
if (isTypeSubtypeOf(targetType, type)) {
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;
if (isTypeSubtypeOf(targetType, constraint)) {
return getIntersectionType([type, targetType]);
}
}
}
}
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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/controlFlowIfStatement.types
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function c<T>(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;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/recursiveTypeRelations.types
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export function css<S extends { [K in keyof S]: string }>(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
Expand Down
32 changes: 32 additions & 0 deletions tests/baselines/reference/strictTypeofUnionNarrowing.js
Original file line number Diff line number Diff line change
@@ -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() : "";
}
47 changes: 47 additions & 0 deletions tests/baselines/reference/strictTypeofUnionNarrowing.symbols
Original file line number Diff line number Diff line change
@@ -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, --, --))
}

71 changes: 71 additions & 0 deletions tests/baselines/reference/strictTypeofUnionNarrowing.types
Original file line number Diff line number Diff line change
@@ -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
>"" : ""
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -34,7 +34,7 @@ if (typeof a === "boolean") {

let c: boolean = a;
>c : boolean
>a : boolean
>a : (false & {}) | (true & {})
}

if (typeof b === "number") {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -65,6 +65,6 @@ if (typeof b === "boolean") {

let c: boolean = b;
>c : boolean
>b : boolean
>b : ({ toString(): string; } & false) | ({ toString(): string; } & true)
}

Loading