From 20d8a94d66ce467053bce183b0a1e0538a1ea96d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 25 Feb 2016 03:32:15 -0500 Subject: [PATCH 1/7] Remove check narrowing only certain types, add test showing issues with this --- .../typeGuardNarrowsPrimitiveIntersection.js | 38 ++++++++++ ...eGuardNarrowsPrimitiveIntersection.symbols | 70 +++++++++++++++++ ...ypeGuardNarrowsPrimitiveIntersection.types | 76 +++++++++++++++++++ .../typeGuardNarrowsPrimitiveIntersection.ts | 21 +++++ 4 files changed, 205 insertions(+) create mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js create mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js new file mode 100644 index 0000000000000..a4cba6f4ab5c1 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js @@ -0,0 +1,38 @@ +//// [typeGuardNarrowsPrimitiveIntersection.ts] +type Tag = {__tag: any}; +declare function isNonBlank(value: string) : value is (string & Tag); +declare function doThis(value: string & Tag): void; +declare function doThat(value: string) : void; +let value: string; +if (isNonBlank(value)) { + doThis(value); +} else { + doThat(value); +} + + +const enum Tag2 {} +declare function isNonBlank2(value: string) : value is (string & Tag2); +declare function doThis2(value: string & Tag2): void; +declare function doThat2(value: string) : void; +if (isNonBlank2(value)) { + doThis2(value); +} else { + doThat2(value); +} + + +//// [typeGuardNarrowsPrimitiveIntersection.js] +var value; +if (isNonBlank(value)) { + doThis(value); +} +else { + doThat(value); +} +if (isNonBlank2(value)) { + doThis2(value); +} +else { + doThat2(value); +} diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols new file mode 100644 index 0000000000000..da507fb94c5dc --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols @@ -0,0 +1,70 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts === +type Tag = {__tag: any}; +>Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0)) +>__tag : Symbol(__tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 12)) + +declare function isNonBlank(value: string) : value is (string & Tag); +>isNonBlank : Symbol(isNonBlank, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 24)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 28)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 28)) +>Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0)) + +declare function doThis(value: string & Tag): void; +>doThis : Symbol(doThis, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 69)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 24)) +>Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0)) + +declare function doThat(value: string) : void; +>doThat : Symbol(doThat, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 51)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 3, 24)) + +let value: string; +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + +if (isNonBlank(value)) { +>isNonBlank : Symbol(isNonBlank, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 24)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + + doThis(value); +>doThis : Symbol(doThis, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 69)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + +} else { + doThat(value); +>doThat : Symbol(doThat, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 51)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) +} + + +const enum Tag2 {} +>Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1)) + +declare function isNonBlank2(value: string) : value is (string & Tag2); +>isNonBlank2 : Symbol(isNonBlank2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 12, 18)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 29)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 29)) +>Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1)) + +declare function doThis2(value: string & Tag2): void; +>doThis2 : Symbol(doThis2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 71)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 25)) +>Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1)) + +declare function doThat2(value: string) : void; +>doThat2 : Symbol(doThat2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 53)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 15, 25)) + +if (isNonBlank2(value)) { +>isNonBlank2 : Symbol(isNonBlank2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 12, 18)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + + doThis2(value); +>doThis2 : Symbol(doThis2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 71)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + +} else { + doThat2(value); +>doThat2 : Symbol(doThat2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 53)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) +} + diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types new file mode 100644 index 0000000000000..478363669b461 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types @@ -0,0 +1,76 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts === +type Tag = {__tag: any}; +>Tag : { __tag: any; } +>__tag : any + +declare function isNonBlank(value: string) : value is (string & Tag); +>isNonBlank : (value: string) => value is string & { __tag: any; } +>value : string +>value : any +>Tag : { __tag: any; } + +declare function doThis(value: string & Tag): void; +>doThis : (value: string & { __tag: any; }) => void +>value : string & { __tag: any; } +>Tag : { __tag: any; } + +declare function doThat(value: string) : void; +>doThat : (value: string) => void +>value : string + +let value: string; +>value : string + +if (isNonBlank(value)) { +>isNonBlank(value) : boolean +>isNonBlank : (value: string) => value is string & { __tag: any; } +>value : string + + doThis(value); +>doThis(value) : void +>doThis : (value: string & { __tag: any; }) => void +>value : string & { __tag: any; } + +} else { + doThat(value); +>doThat(value) : void +>doThat : (value: string) => void +>value : string +} + + +const enum Tag2 {} +>Tag2 : Tag2 + +declare function isNonBlank2(value: string) : value is (string & Tag2); +>isNonBlank2 : (value: string) => value is string & Tag2 +>value : string +>value : any +>Tag2 : Tag2 + +declare function doThis2(value: string & Tag2): void; +>doThis2 : (value: string & Tag2) => void +>value : string & Tag2 +>Tag2 : Tag2 + +declare function doThat2(value: string) : void; +>doThat2 : (value: string) => void +>value : string + +if (isNonBlank2(value)) { +>isNonBlank2(value) : boolean +>isNonBlank2 : (value: string) => value is string & Tag2 +>value : string + + doThis2(value); +>doThis2(value) : void +>doThis2 : (value: string & Tag2) => void +>value : string & Tag2 + +} else { + doThat2(value); +>doThat2(value) : void +>doThat2 : (value: string) => void +>value : string +} + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts new file mode 100644 index 0000000000000..376a78275c83b --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts @@ -0,0 +1,21 @@ +type Tag = {__tag: any}; +declare function isNonBlank(value: string) : value is (string & Tag); +declare function doThis(value: string & Tag): void; +declare function doThat(value: string) : void; +let value: string; +if (isNonBlank(value)) { + doThis(value); +} else { + doThat(value); +} + + +const enum Tag2 {} +declare function isNonBlank2(value: string) : value is (string & Tag2); +declare function doThis2(value: string & Tag2): void; +declare function doThat2(value: string) : void; +if (isNonBlank2(value)) { + doThis2(value); +} else { + doThat2(value); +} From a2feb0ee7dc07aad3ef9d05e2ea151c547fd460d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Mar 2016 02:24:26 -0500 Subject: [PATCH 2/7] string literal case test --- .../typeGuardNarrowsToLiteralType.js | 21 +++++++++++ .../typeGuardNarrowsToLiteralType.symbols | 32 +++++++++++++++++ .../typeGuardNarrowsToLiteralType.types | 35 +++++++++++++++++++ .../typeGuardNarrowsToLiteralType.ts | 10 ++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.js create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.js b/tests/baselines/reference/typeGuardNarrowsToLiteralType.js new file mode 100644 index 0000000000000..fdd2ee3fb7f75 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralType.js @@ -0,0 +1,21 @@ +//// [typeGuardNarrowsToLiteralType.ts] +declare function isFoo(value: string) : value is "foo"; +declare function doThis(value: "foo"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + + + +//// [typeGuardNarrowsToLiteralType.js] +var value; +if (isFoo(value)) { + doThis(value); +} +else { + doThat(value); +} diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols b/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols new file mode 100644 index 0000000000000..0d0ddc5f00dc1 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts === +declare function isFoo(value: string) : value is "foo"; +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23)) + +declare function doThis(value: "foo"): void; +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 1, 24)) + +declare function doThat(value: string) : void; +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 2, 24)) + +let value: string; +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) + +if (isFoo(value)) { +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) + + doThis(value); +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) + +} else { + doThat(value); +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) +} + + diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.types b/tests/baselines/reference/typeGuardNarrowsToLiteralType.types new file mode 100644 index 0000000000000..9835206deb9f4 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralType.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts === +declare function isFoo(value: string) : value is "foo"; +>isFoo : (value: string) => value is "foo" +>value : string +>value : any + +declare function doThis(value: "foo"): void; +>doThis : (value: "foo") => void +>value : "foo" + +declare function doThat(value: string) : void; +>doThat : (value: string) => void +>value : string + +let value: string; +>value : string + +if (isFoo(value)) { +>isFoo(value) : boolean +>isFoo : (value: string) => value is "foo" +>value : string + + doThis(value); +>doThis(value) : void +>doThis : (value: "foo") => void +>value : "foo" + +} else { + doThat(value); +>doThat(value) : void +>doThat : (value: string) => void +>value : string +} + + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts new file mode 100644 index 0000000000000..3b7d5bba21ab7 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts @@ -0,0 +1,10 @@ +declare function isFoo(value: string) : value is "foo"; +declare function doThis(value: "foo"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + From 92bf91dd581b98e9c9fd0c8d69dae6132af762ef Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:14:00 -0400 Subject: [PATCH 3/7] Reconcile fix with CFA work --- src/compiler/checker.ts | 4 ++-- src/compiler/types.ts | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6a42dc43d5159..ca0d61c810603 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7555,7 +7555,7 @@ namespace ts { } function getNarrowedTypeOfReference(type: Type, reference: Node) { - if (!(type.flags & TypeFlags.Narrowable) || !isNarrowableReference(reference)) { + if (type.flags & TypeFlags.Defaultable || !isNarrowableReference(reference)) { return type; } const leftmostNode = getLeftmostIdentifierOrThis(reference); @@ -7975,7 +7975,7 @@ namespace ts { const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - if (defaultsToDeclaredType && !(type.flags & TypeFlags.Narrowable)) { + if (defaultsToDeclaredType && type.flags & TypeFlags.Defaultable) { return type; } const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d0d411a710eb3..5f181bc982e77 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2175,7 +2175,13 @@ namespace ts { ObjectType = Class | Interface | Reference | Tuple | Anonymous, UnionOrIntersection = Union | Intersection, StructuredType = ObjectType | Union | Intersection, - Narrowable = Any | ObjectType | Union | TypeParameter, + + // 'Defaultable' types are types where narrowing reverts to the original type, rather than actually narrow. + // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep + // Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value) + // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing + // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules) + Defaultable = Void, /* @internal */ RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, /* @internal */ From 73d4aaef46eea2323587edb650678c2fe70aa170 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:17:40 -0400 Subject: [PATCH 4/7] Defaultable -> NotNarrowable to align with use --- src/compiler/checker.ts | 4 ++-- src/compiler/types.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ca0d61c810603..2e2f59f6c155e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7555,7 +7555,7 @@ namespace ts { } function getNarrowedTypeOfReference(type: Type, reference: Node) { - if (type.flags & TypeFlags.Defaultable || !isNarrowableReference(reference)) { + if (type.flags & TypeFlags.NotNarrowable || !isNarrowableReference(reference)) { return type; } const leftmostNode = getLeftmostIdentifierOrThis(reference); @@ -7975,7 +7975,7 @@ namespace ts { const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - if (defaultsToDeclaredType && type.flags & TypeFlags.Defaultable) { + if (defaultsToDeclaredType && type.flags & TypeFlags.NotNarrowable) { return type; } const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5f181bc982e77..47f9d1b752e55 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2176,12 +2176,12 @@ namespace ts { UnionOrIntersection = Union | Intersection, StructuredType = ObjectType | Union | Intersection, - // 'Defaultable' types are types where narrowing reverts to the original type, rather than actually narrow. + // 'NotNarrowable' types are types where narrowing reverts to the original type, rather than actually narrow. // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep // Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value) // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules) - Defaultable = Void, + NotNarrowable = Void, /* @internal */ RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, /* @internal */ From 70733da2f2013ef786486d64a6dce7efae504298 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:23:54 -0400 Subject: [PATCH 5/7] Missed a defaultable in comments --- src/compiler/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 47f9d1b752e55..044cf3f4e8b97 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2178,7 +2178,7 @@ namespace ts { // 'NotNarrowable' types are types where narrowing reverts to the original type, rather than actually narrow. // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep - // Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value) + // Void as the only non-narrowable type, since it's a non-value type construct (representing a lack of a value) // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules) NotNarrowable = Void, From a80529b9899ebe105f6e72a176f4bdf6c5eb1586 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:37:00 -0400 Subject: [PATCH 6/7] Add test for narrowing to unions of string literals --- .../typeGuardNarrowsToLiteralTypeUnion.js | 21 +++++++++++ ...typeGuardNarrowsToLiteralTypeUnion.symbols | 32 +++++++++++++++++ .../typeGuardNarrowsToLiteralTypeUnion.types | 35 +++++++++++++++++++ .../typeGuardNarrowsToLiteralTypeUnion.ts | 10 ++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js new file mode 100644 index 0000000000000..715b362c8e0c2 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js @@ -0,0 +1,21 @@ +//// [typeGuardNarrowsToLiteralTypeUnion.ts] +declare function isFoo(value: string) : value is ("foo" | "bar"); +declare function doThis(value: "foo" | "bar"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + + + +//// [typeGuardNarrowsToLiteralTypeUnion.js] +var value; +if (isFoo(value)) { + doThis(value); +} +else { + doThat(value); +} diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols new file mode 100644 index 0000000000000..356fa06a06c4a --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts === +declare function isFoo(value: string) : value is ("foo" | "bar"); +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23)) + +declare function doThis(value: "foo" | "bar"): void; +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 24)) + +declare function doThat(value: string) : void; +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 2, 24)) + +let value: string; +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) + +if (isFoo(value)) { +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) + + doThis(value); +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) + +} else { + doThat(value); +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) +} + + diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types new file mode 100644 index 0000000000000..321a8861d55f5 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts === +declare function isFoo(value: string) : value is ("foo" | "bar"); +>isFoo : (value: string) => value is "foo" | "bar" +>value : string +>value : any + +declare function doThis(value: "foo" | "bar"): void; +>doThis : (value: "foo" | "bar") => void +>value : "foo" | "bar" + +declare function doThat(value: string) : void; +>doThat : (value: string) => void +>value : string + +let value: string; +>value : string + +if (isFoo(value)) { +>isFoo(value) : boolean +>isFoo : (value: string) => value is "foo" | "bar" +>value : string + + doThis(value); +>doThis(value) : void +>doThis : (value: "foo" | "bar") => void +>value : "foo" | "bar" + +} else { + doThat(value); +>doThat(value) : void +>doThat : (value: string) => void +>value : string +} + + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts new file mode 100644 index 0000000000000..8f4726160f4b2 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts @@ -0,0 +1,10 @@ +declare function isFoo(value: string) : value is ("foo" | "bar"); +declare function doThis(value: "foo" | "bar"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + From 9a620bf616e1f6fac6d035ce43ad690455fa84a6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 1 Jun 2016 11:39:22 -0700 Subject: [PATCH 7/7] Actually merge from master --- src/compiler/checker.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 52e70941408ef..ef11dc904ea19 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8156,23 +8156,12 @@ namespace ts { return type; } const declaration = localOrExportSymbol.valueDeclaration; -<<<<<<< HEAD - const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || - declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || - getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - if (defaultsToDeclaredType && type.flags & TypeFlags.NotNarrowable) { - return type; - } - const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); - if (strictNullChecks && !(type.flags & TypeFlags.Any) && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) { -======= const includeOuterFunctions = isReadonlySymbol(localOrExportSymbol); const assumeInitialized = !strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || !declaration || getRootDeclaration(declaration).kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || !isDeclarationIncludedInFlow(node, declaration, includeOuterFunctions); const flowType = getFlowTypeOfReference(node, type, assumeInitialized, includeOuterFunctions); if (!assumeInitialized && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) { ->>>>>>> master error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); // Return the declared type to reduce follow-on errors return type;