mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Merge pull request #13791 from Microsoft/typeof-string-literal-union-type
`typeof x` now has a string literal union type
This commit is contained in:
@@ -326,7 +326,6 @@ namespace ts {
|
||||
"object": TypeFacts.TypeofEQObject,
|
||||
"function": TypeFacts.TypeofEQFunction
|
||||
});
|
||||
|
||||
const typeofNEFacts = createMapFromTemplate({
|
||||
"string": TypeFacts.TypeofNEString,
|
||||
"number": TypeFacts.TypeofNENumber,
|
||||
@@ -336,7 +335,6 @@ namespace ts {
|
||||
"object": TypeFacts.TypeofNEObject,
|
||||
"function": TypeFacts.TypeofNEFunction
|
||||
});
|
||||
|
||||
const typeofTypesByName = createMapFromTemplate<Type>({
|
||||
"string": stringType,
|
||||
"number": numberType,
|
||||
@@ -344,6 +342,7 @@ namespace ts {
|
||||
"symbol": esSymbolType,
|
||||
"undefined": undefinedType
|
||||
});
|
||||
const typeofType = createTypeofType();
|
||||
|
||||
let jsxElementType: Type;
|
||||
let _jsxNamespace: string;
|
||||
@@ -1727,6 +1726,10 @@ namespace ts {
|
||||
return type;
|
||||
}
|
||||
|
||||
function createTypeofType() {
|
||||
return getUnionType(convertToArray(typeofEQFacts.keys(), s => getLiteralTypeForText(TypeFlags.StringLiteral, s)));
|
||||
}
|
||||
|
||||
// A reserved member name starts with two underscores, but the third character cannot be an underscore
|
||||
// or the @ symbol. A third underscore indicates an escaped form of an identifer that started
|
||||
// with at least two underscores. The @ character indicates that the name is denoted by a well known ES
|
||||
@@ -14626,7 +14629,7 @@ namespace ts {
|
||||
|
||||
function checkTypeOfExpression(node: TypeOfExpression): Type {
|
||||
checkExpression(node.expression);
|
||||
return stringType;
|
||||
return typeofType;
|
||||
}
|
||||
|
||||
function checkVoidExpression(node: VoidExpression): Type {
|
||||
|
||||
@@ -895,6 +895,14 @@ namespace ts {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function convertToArray<T, U>(iterator: Iterator<T>, f: (value: T) => U) {
|
||||
const result: U[] = [];
|
||||
for (let { value, done } = iterator.next(); !done; { value, done } = iterator.next()) {
|
||||
result.push(f(value));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls `callback` for each entry in the map, returning the first truthy result.
|
||||
* Use `map.forEach` instead for normal iteration.
|
||||
|
||||
Reference in New Issue
Block a user