From 7bb2ee56d050c6dd2d1525cbbe0ebdfb6aad4423 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Sun, 3 Jan 2016 07:43:30 -0500 Subject: [PATCH] Fix #6277 - stop looking for `any` specifically, and use isTypeSubtypeOf like the old code --- src/compiler/checker.ts | 8 +-- .../typeGuardOfFormTypeOfPrimitiveSubtype.js | 45 ++++++++++++ ...eGuardOfFormTypeOfPrimitiveSubtype.symbols | 52 ++++++++++++++ ...ypeGuardOfFormTypeOfPrimitiveSubtype.types | 70 +++++++++++++++++++ .../typeGuardOfFormTypeOfPrimitiveSubtype.ts | 21 ++++++ 5 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.js create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.symbols create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfPrimitiveSubtype.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5d57e9d202874..2dc62443d06f2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6674,10 +6674,6 @@ namespace ts { if (typeInfo && typeInfo.type === undefinedType) { return type; } - // If the type to be narrowed is any and we're checking a primitive with assumeTrue=true, return the primitive - if (!!(type.flags & TypeFlags.Any) && typeInfo && assumeTrue) { - return typeInfo.type; - } let flags: TypeFlags; if (typeInfo) { flags = typeInfo.flags; @@ -6688,6 +6684,10 @@ namespace ts { } // At this point we can bail if it's not a union if (!(type.flags & TypeFlags.Union)) { + // If we're on the true branch and the type is a subtype, we should return the primitive type + if (assumeTrue && typeInfo && isTypeSubtypeOf(typeInfo.type, type)) { + return typeInfo.type; + } // If the active non-union type would be removed from a union by this type guard, return an empty union return filterUnion(type) ? type : emptyUnionType; } diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.js b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.js new file mode 100644 index 0000000000000..0bd0389017910 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.js @@ -0,0 +1,45 @@ +//// [typeGuardOfFormTypeOfPrimitiveSubtype.ts] +let a: {}; +let b: {toString(): string}; +if (typeof a === "number") { + let c: number = a; +} +if (typeof a === "string") { + let c: string = a; +} +if (typeof a === "boolean") { + let c: boolean = a; +} + +if (typeof b === "number") { + let c: number = b; +} +if (typeof b === "string") { + let c: string = b; +} +if (typeof b === "boolean") { + let c: boolean = b; +} + + +//// [typeGuardOfFormTypeOfPrimitiveSubtype.js] +var a; +var b; +if (typeof a === "number") { + var c = a; +} +if (typeof a === "string") { + var c = a; +} +if (typeof a === "boolean") { + var c = a; +} +if (typeof b === "number") { + var c = b; +} +if (typeof b === "string") { + var c = b; +} +if (typeof b === "boolean") { + var c = b; +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.symbols b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.symbols new file mode 100644 index 0000000000000..da962b6060fad --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfPrimitiveSubtype.ts === +let a: {}; +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) + +let b: {toString(): string}; +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) +>toString : Symbol(toString, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 8)) + +if (typeof a === "number") { +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) + + let c: number = a; +>c : Symbol(c, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 3, 7)) +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) +} +if (typeof a === "string") { +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) + + let c: string = a; +>c : Symbol(c, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 6, 7)) +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) +} +if (typeof a === "boolean") { +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) + + let c: boolean = a; +>c : Symbol(c, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 9, 7)) +>a : Symbol(a, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 0, 3)) +} + +if (typeof b === "number") { +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) + + let c: number = b; +>c : Symbol(c, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 13, 7)) +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) +} +if (typeof b === "string") { +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) + + let c: string = b; +>c : Symbol(c, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 16, 7)) +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) +} +if (typeof b === "boolean") { +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) + + let c: boolean = b; +>c : Symbol(c, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 19, 7)) +>b : Symbol(b, Decl(typeGuardOfFormTypeOfPrimitiveSubtype.ts, 1, 3)) +} + diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types new file mode 100644 index 0000000000000..7e88ca5cb94e5 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfPrimitiveSubtype.types @@ -0,0 +1,70 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfPrimitiveSubtype.ts === +let a: {}; +>a : {} + +let b: {toString(): string}; +>b : { toString(): string; } +>toString : () => string + +if (typeof a === "number") { +>typeof a === "number" : boolean +>typeof a : string +>a : {} +>"number" : string + + let c: number = a; +>c : number +>a : number +} +if (typeof a === "string") { +>typeof a === "string" : boolean +>typeof a : string +>a : {} +>"string" : string + + let c: string = a; +>c : string +>a : string +} +if (typeof a === "boolean") { +>typeof a === "boolean" : boolean +>typeof a : string +>a : {} +>"boolean" : string + + let c: boolean = a; +>c : boolean +>a : boolean +} + +if (typeof b === "number") { +>typeof b === "number" : boolean +>typeof b : string +>b : { toString(): string; } +>"number" : string + + let c: number = b; +>c : number +>b : number +} +if (typeof b === "string") { +>typeof b === "string" : boolean +>typeof b : string +>b : { toString(): string; } +>"string" : string + + let c: string = b; +>c : string +>b : string +} +if (typeof b === "boolean") { +>typeof b === "boolean" : boolean +>typeof b : string +>b : { toString(): string; } +>"boolean" : string + + let c: boolean = b; +>c : boolean +>b : boolean +} + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfPrimitiveSubtype.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfPrimitiveSubtype.ts new file mode 100644 index 0000000000000..b0493db428e90 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfPrimitiveSubtype.ts @@ -0,0 +1,21 @@ +let a: {}; +let b: {toString(): string}; +if (typeof a === "number") { + let c: number = a; +} +if (typeof a === "string") { + let c: string = a; +} +if (typeof a === "boolean") { + let c: boolean = a; +} + +if (typeof b === "number") { + let c: number = b; +} +if (typeof b === "string") { + let c: string = b; +} +if (typeof b === "boolean") { + let c: boolean = b; +}