Skip to content
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

typeof x now has a string literal union type #13791

Merged
merged 6 commits into from
Feb 7, 2017
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
9 changes: 6 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ namespace ts {
"object": TypeFacts.TypeofEQObject,
"function": TypeFacts.TypeofEQFunction
});

const typeofNEFacts = createMapFromTemplate({
"string": TypeFacts.TypeofNEString,
"number": TypeFacts.TypeofNENumber,
Expand All @@ -336,14 +335,14 @@ namespace ts {
"object": TypeFacts.TypeofNEObject,
"function": TypeFacts.TypeofNEFunction
});

const typeofTypesByName = createMapFromTemplate<Type>({
"string": stringType,
"number": numberType,
"boolean": booleanType,
"symbol": esSymbolType,
"undefined": undefinedType
});
const typeofType = createTypeofType();

let jsxElementType: Type;
let _jsxNamespace: string;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -14626,7 +14629,7 @@ namespace ts {

function checkTypeOfExpression(node: TypeOfExpression): Type {
checkExpression(node.expression);
return stringType;
return typeofType;
}

function checkVoidExpression(node: VoidExpression): Type {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/TypeGuardWithEnumUnion.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function f1(x: Color | string) {

if (typeof x === "number") {
>typeof x === "number" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | Color
>"number" : "number"

Expand Down Expand Up @@ -41,7 +41,7 @@ function f2(x: Color | string | string[]) {

if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | Color | string[]
>"object" : "object"

Expand All @@ -54,7 +54,7 @@ function f2(x: Color | string | string[]) {
}
if (typeof x === "number") {
>typeof x === "number" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | Color | string[]
>"number" : "number"

Expand All @@ -76,7 +76,7 @@ function f2(x: Color | string | string[]) {
}
if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | Color | string[]
>"string" : "string"

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/anonymousClassExpression1.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function f() {

return typeof class {} === "function";
>typeof class {} === "function" : boolean
>typeof class {} : string
>typeof class {} : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>class {} : typeof (Anonymous class)
>"function" : "function"
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ var r6 = foo6(1);
>1 : 1

function foo7(x) {
>foo7 : (x: any) => string
>foo7 : (x: any) => "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : any

return typeof x;
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : any
}
var r7 = foo7(1);
>r7 : string
>foo7(1) : string
>foo7 : (x: any) => string
>r7 : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>foo7(1) : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>foo7 : (x: any) => "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>1 : 1

// object types
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/castExpressionParentheses.types
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ declare var A;
>(<any>typeof A).x : any
>(<any>typeof A) : any
><any>typeof A : any
>typeof A : string
>typeof A : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>A : any
>x : any

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/castOfAwait.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function f() {
>0 : 0

typeof await 0;
>typeof await 0 : string
>typeof await 0 : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>await 0 : 0
>0 : 0

Expand All @@ -21,7 +21,7 @@ async function f() {
>await void <string> typeof <number> void await 0 : any
>void <string> typeof <number> void await 0 : undefined
><string> typeof <number> void await 0 : string
>typeof <number> void await 0 : string
>typeof <number> void await 0 : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
><number> void await 0 : number
>void await 0 : undefined
>await 0 : 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var x: StringTree;

if (typeof x !== "string") {
>typeof x !== "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : StringTree
>"string" : "string"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ true ? exprString1 : exprBoolean1; // union
typeof "123" == "string" ? exprBoolean1 : exprBoolean2;
>typeof "123" == "string" ? exprBoolean1 : exprBoolean2 : boolean
>typeof "123" == "string" : boolean
>typeof "123" : string
>typeof "123" : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>"123" : "123"
>"string" : "string"
>exprBoolean1 : boolean
Expand Down Expand Up @@ -262,7 +262,7 @@ var resultIsBoolean3 = typeof "123" == "string" ? exprBoolean1 : exprBoolean2;
>resultIsBoolean3 : boolean
>typeof "123" == "string" ? exprBoolean1 : exprBoolean2 : boolean
>typeof "123" == "string" : boolean
>typeof "123" : string
>typeof "123" : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>"123" : "123"
>"string" : "string"
>exprBoolean1 : boolean
Expand Down Expand Up @@ -299,7 +299,7 @@ var resultIsStringOrBoolean4 = typeof "123" === "string" ? exprString1 : exprBoo
>resultIsStringOrBoolean4 : string | boolean
>typeof "123" === "string" ? exprString1 : exprBoolean1 : string | boolean
>typeof "123" === "string" : boolean
>typeof "123" : string
>typeof "123" : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>"123" : "123"
>"string" : "string"
>exprString1 : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ var array = ["1", "2", "3"];

typeof condString ? exprAny1 : exprAny2;
>typeof condString ? exprAny1 : exprAny2 : any
>typeof condString : string
>typeof condString : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>condString : string
>exprAny1 : any
>exprAny2 : any
Expand Down Expand Up @@ -254,7 +254,7 @@ var resultIsStringOrBoolean2 = "hello" ? exprString1 : exprBoolean1; // union
var resultIsAny3 = typeof condString ? exprAny1 : exprAny2;
>resultIsAny3 : any
>typeof condString ? exprAny1 : exprAny2 : any
>typeof condString : string
>typeof condString : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>condString : string
>exprAny1 : any
>exprAny2 : any
Expand Down Expand Up @@ -297,7 +297,7 @@ var resultIsObject3 = array[1] ? exprIsObject1 : exprIsObject2;
var resultIsStringOrBoolean3 = typeof condString ? exprString1 : exprBoolean1; // union
>resultIsStringOrBoolean3 : string | boolean
>typeof condString ? exprString1 : exprBoolean1 : string | boolean
>typeof condString : string
>typeof condString : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>condString : string
>exprString1 : string
>exprBoolean1 : boolean
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/constLocalsInFunctionExpressions.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function f1() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand All @@ -35,7 +35,7 @@ function f2() {

if (typeof x !== "string") {
>typeof x !== "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand All @@ -59,7 +59,7 @@ function f3() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand All @@ -82,7 +82,7 @@ function f4() {

if (typeof x !== "string") {
>typeof x !== "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand All @@ -106,7 +106,7 @@ function f5() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/controlFlowCommaOperator.types
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function f(x: string | number | boolean) {
>y : string | number | boolean
>"" : ""
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number | boolean
>"string" : "string"

Expand All @@ -36,7 +36,7 @@ function f(x: string | number | boolean) {
>z : string | number | boolean
>1 : 1
>typeof x === "number" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | boolean
>"number" : "number"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function c() {

if (typeof x === "string") continue;
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/controlFlowForStatement.types
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function d() {
>x : string | number | boolean
>"" : ""
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"
>x = 5 : 5
Expand All @@ -107,7 +107,7 @@ function e() {
>"" : ""
>0 : 0
>typeof x !== "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | true
>"string" : "string"
>x = "" || true : true
Expand All @@ -128,7 +128,7 @@ function f() {

for (; typeof x !== "string";) {
>typeof x !== "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number | boolean
>"string" : "string"

Expand All @@ -137,7 +137,7 @@ function f() {

if (typeof x === "number") break;
>typeof x === "number" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | boolean
>"number" : "number"

Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/controlFlowIIFE.types
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function f1() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down Expand Up @@ -41,7 +41,7 @@ function f2() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down Expand Up @@ -73,7 +73,7 @@ function f3() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/controlFlowIfStatement.types
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function c<T>(data: string | T): T {

if (typeof data === 'string') {
>typeof data === 'string' : boolean
>typeof data : string
>typeof data : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>data : string | T
>'string' : "string"

Expand All @@ -124,7 +124,7 @@ function d<T extends string>(data: string | T): never {

if (typeof data === 'string') {
>typeof data === 'string' : boolean
>typeof data : string
>typeof data : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>data : string | T
>'string' : "string"

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/controlFlowWhileStatement.types
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function c() {

if (typeof x === "string") continue;
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function foo1() {

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"

Expand Down Expand Up @@ -49,7 +49,7 @@ function foo2() {

if (typeof x === "number") {
>typeof x === "number" : boolean
>typeof x : string
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"number" : "number"

Expand Down
Loading