Support unknown types (host object names) in typeof type guards

This commit is contained in:
Anders Hejlsberg 2016-03-31 10:07:28 -07:00
parent 3d0fa31a9d
commit ce81ba5156

View file

@ -202,38 +202,40 @@ namespace ts {
TypeofEQSymbol = 1 << 3, // typeof x === "symbol" TypeofEQSymbol = 1 << 3, // typeof x === "symbol"
TypeofEQObject = 1 << 4, // typeof x === "object" TypeofEQObject = 1 << 4, // typeof x === "object"
TypeofEQFunction = 1 << 5, // typeof x === "function" TypeofEQFunction = 1 << 5, // typeof x === "function"
TypeofNEString = 1 << 6, // typeof x !== "string" TypeofEQHostObject = 1 << 6, // typeof x === "xxx"
TypeofNENumber = 1 << 7, // typeof x !== "number" TypeofNEString = 1 << 7, // typeof x !== "string"
TypeofNEBoolean = 1 << 8, // typeof x !== "boolean" TypeofNENumber = 1 << 8, // typeof x !== "number"
TypeofNESymbol = 1 << 9, // typeof x !== "symbol" TypeofNEBoolean = 1 << 9, // typeof x !== "boolean"
TypeofNEObject = 1 << 10, // typeof x !== "object" TypeofNESymbol = 1 << 10, // typeof x !== "symbol"
TypeofNEFunction = 1 << 11, // typeof x !== "function" TypeofNEObject = 1 << 11, // typeof x !== "object"
EQUndefined = 1 << 12, // x === undefined TypeofNEFunction = 1 << 12, // typeof x !== "function"
EQNull = 1 << 13, // x === null TypeofNEHostObject = 1 << 13, // typeof x !== "xxx"
EQUndefinedOrNull = 1 << 14, // x == undefined / x == null EQUndefined = 1 << 14, // x === undefined
NEUndefined = 1 << 15, // x !== undefined EQNull = 1 << 15, // x === null
NENull = 1 << 16, // x !== null EQUndefinedOrNull = 1 << 16, // x == undefined / x == null
NEUndefinedOrNull = 1 << 17, // x != undefined / x != null NEUndefined = 1 << 17, // x !== undefined
Truthy = 1 << 18, // x NENull = 1 << 18, // x !== null
Falsy = 1 << 19, // !x NEUndefinedOrNull = 1 << 19, // x != undefined / x != null
All = (1 << 20) - 1, 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 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 // The presence of a particular fact means that the given test is true for some (and possibly all) values
// of that kind of type. // 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, 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, 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, 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, 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, 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, FunctionFacts = FunctionStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | EQUndefined | EQUndefinedOrNull | NENull | Falsy, UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | EQUndefined | EQUndefinedOrNull | NENull | Falsy,
NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | EQNull | EQUndefinedOrNull | NEUndefined | Falsy, NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy,
} }
const typeofEQFacts: Map<TypeFacts> = { const typeofEQFacts: Map<TypeFacts> = {
@ -7551,8 +7553,8 @@ namespace ts {
} }
} }
const facts = assumeTrue ? const facts = assumeTrue ?
getProperty(typeofEQFacts, right.text) || TypeFacts.TypeofEQObject : getProperty(typeofEQFacts, right.text) || TypeFacts.TypeofEQHostObject :
getProperty(typeofNEFacts, right.text) || TypeFacts.TypeofNEObject; getProperty(typeofNEFacts, right.text) || TypeFacts.TypeofNEHostObject;
return getTypeWithFacts(type, facts); return getTypeWithFacts(type, facts);
} }