From ce81ba51566a328f6ee7c9f41f570675bea7ca3f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 31 Mar 2016 10:07:28 -0700 Subject: [PATCH] Support unknown types (host object names) in typeof type guards --- src/compiler/checker.ts | 64 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1e05702d978..da6548f8c69 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -196,44 +196,46 @@ namespace ts { const enum TypeFacts { None = 0, - TypeofEQString = 1 << 0, // typeof x === "string" - TypeofEQNumber = 1 << 1, // typeof x === "number" - TypeofEQBoolean = 1 << 2, // typeof x === "boolean" - TypeofEQSymbol = 1 << 3, // typeof x === "symbol" - TypeofEQObject = 1 << 4, // typeof x === "object" - TypeofEQFunction = 1 << 5, // typeof x === "function" - TypeofNEString = 1 << 6, // typeof x !== "string" - TypeofNENumber = 1 << 7, // typeof x !== "number" - TypeofNEBoolean = 1 << 8, // typeof x !== "boolean" - TypeofNESymbol = 1 << 9, // typeof x !== "symbol" - TypeofNEObject = 1 << 10, // typeof x !== "object" - TypeofNEFunction = 1 << 11, // typeof x !== "function" - EQUndefined = 1 << 12, // x === undefined - EQNull = 1 << 13, // x === null - EQUndefinedOrNull = 1 << 14, // x == undefined / x == null - NEUndefined = 1 << 15, // x !== undefined - NENull = 1 << 16, // x !== null - NEUndefinedOrNull = 1 << 17, // x != undefined / x != null - Truthy = 1 << 18, // x - Falsy = 1 << 19, // !x - All = (1 << 20) - 1, + TypeofEQString = 1 << 0, // typeof x === "string" + TypeofEQNumber = 1 << 1, // typeof x === "number" + TypeofEQBoolean = 1 << 2, // typeof x === "boolean" + TypeofEQSymbol = 1 << 3, // typeof x === "symbol" + TypeofEQObject = 1 << 4, // typeof x === "object" + TypeofEQFunction = 1 << 5, // typeof x === "function" + TypeofEQHostObject = 1 << 6, // typeof x === "xxx" + TypeofNEString = 1 << 7, // typeof x !== "string" + TypeofNENumber = 1 << 8, // typeof x !== "number" + TypeofNEBoolean = 1 << 9, // typeof x !== "boolean" + TypeofNESymbol = 1 << 10, // typeof x !== "symbol" + TypeofNEObject = 1 << 11, // typeof x !== "object" + TypeofNEFunction = 1 << 12, // typeof x !== "function" + TypeofNEHostObject = 1 << 13, // typeof x !== "xxx" + EQUndefined = 1 << 14, // x === undefined + EQNull = 1 << 15, // x === null + EQUndefinedOrNull = 1 << 16, // x == undefined / x == null + NEUndefined = 1 << 17, // x !== undefined + NENull = 1 << 18, // x !== null + NEUndefinedOrNull = 1 << 19, // x != undefined / x != null + Truthy = 1 << 20, // x + Falsy = 1 << 21, // !x + All = (1 << 22) - 1, // The following members encode facts about particular kinds of types for use in the getTypeFacts function. // The presence of a particular fact means that the given test is true for some (and possibly all) values // of that kind of type. - StringStrictFacts = TypeofEQString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | NEUndefined | NENull | NEUndefinedOrNull | Truthy | Falsy, + StringStrictFacts = TypeofEQString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy | Falsy, StringFacts = StringStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull, - NumberStrictFacts = TypeofEQNumber | TypeofNEString | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | NEUndefined | NENull | NEUndefinedOrNull | Truthy | Falsy, + NumberStrictFacts = TypeofEQNumber | TypeofNEString | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy | Falsy, NumberFacts = NumberStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull, - BooleanStrictFacts = TypeofEQBoolean | TypeofNEString | TypeofNENumber | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | NEUndefined | NENull | NEUndefinedOrNull | Truthy | Falsy, + BooleanStrictFacts = TypeofEQBoolean | TypeofNEString | TypeofNENumber | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy | Falsy, BooleanFacts = BooleanStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull, - SymbolStrictFacts = TypeofEQSymbol | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNEObject | TypeofNEFunction | NEUndefined | NENull | NEUndefinedOrNull | Truthy, + SymbolStrictFacts = TypeofEQSymbol | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy, SymbolFacts = SymbolStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy, - ObjectStrictFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | NEUndefined | NENull | NEUndefinedOrNull | Truthy, + ObjectStrictFacts = TypeofEQObject | TypeofEQHostObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | NEUndefined | NENull | NEUndefinedOrNull | Truthy, ObjectFacts = ObjectStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy, - FunctionStrictFacts = TypeofEQFunction | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy, + FunctionStrictFacts = TypeofEQFunction | TypeofEQHostObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy, FunctionFacts = FunctionStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy, - UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | EQUndefined | EQUndefinedOrNull | NENull | Falsy, - NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | EQNull | EQUndefinedOrNull | NEUndefined | Falsy, + UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | EQUndefined | EQUndefinedOrNull | NENull | Falsy, + NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy, } const typeofEQFacts: Map = { @@ -7551,8 +7553,8 @@ namespace ts { } } const facts = assumeTrue ? - getProperty(typeofEQFacts, right.text) || TypeFacts.TypeofEQObject : - getProperty(typeofNEFacts, right.text) || TypeFacts.TypeofNEObject; + getProperty(typeofEQFacts, right.text) || TypeFacts.TypeofEQHostObject : + getProperty(typeofNEFacts, right.text) || TypeFacts.TypeofNEHostObject; return getTypeWithFacts(type, facts); }