Skip to content

Fixes non-null assertion applied to type narrowed to never not issuing an error. #35863

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

Merged
merged 1 commit into from
Mar 11, 2021
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22185,7 +22185,7 @@ namespace ts {
// on empty arrays are possible without implicit any errors and new element types can be inferred without
// type mismatch errors.
const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType);
if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) {
if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && !(resultType.flags & TypeFlags.Never) && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) {
return declaredType;
}
return resultType;
Expand Down
18 changes: 16 additions & 2 deletions tests/baselines/reference/exhaustiveSwitchStatements1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(7,9): error TS7027: Unreachable code detected.
tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(235,5): error TS2367: This condition will always return 'false' since the types 'keyof O' and '"c"' have no overlap.
tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(247,10): error TS2339: Property 'kind' does not exist on type 'never'.


==== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts (2 errors) ====
==== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts (3 errors) ====
function f1(x: 1 | 2): string {
if (!!true) {
switch (x) {
Expand Down Expand Up @@ -244,4 +245,17 @@ tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(235,5): error
!!! error TS2367: This condition will always return 'false' since the types 'keyof O' and '"c"' have no overlap.
return o[k];
}


// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };

function f35431(a: A) {
switch (a.kind) {
case "abc":
case "def": return;
default:
a!.kind; // Error expected
~~~~
!!! error TS2339: Property 'kind' does not exist on type 'never'.
}
}
27 changes: 26 additions & 1 deletion tests/baselines/reference/exhaustiveSwitchStatements1.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,18 @@ function ff(o: O, k: K) {
k === 'c'; // Error
return o[k];
}


// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };

function f35431(a: A) {
switch (a.kind) {
case "abc":
case "def": return;
default:
a!.kind; // Error expected
}
}

//// [exhaustiveSwitchStatements1.js]
"use strict";
Expand Down Expand Up @@ -453,6 +464,14 @@ function ff(o, k) {
k === 'c'; // Error
return o[k];
}
function f35431(a) {
switch (a.kind) {
case "abc":
case "def": return;
default:
a.kind; // Error expected
}
}


//// [exhaustiveSwitchStatements1.d.ts]
Expand Down Expand Up @@ -524,3 +543,9 @@ declare type O = {
};
declare type K = keyof O | 'c';
declare function ff(o: O, k: K): number;
declare type A = {
kind: "abc";
} | {
kind: "def";
};
declare function f35431(a: A): void;
23 changes: 23 additions & 0 deletions tests/baselines/reference/exhaustiveSwitchStatements1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,26 @@ function ff(o: O, k: K) {
>k : Symbol(k, Decl(exhaustiveSwitchStatements1.ts, 229, 17))
}

// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };
>A : Symbol(A, Decl(exhaustiveSwitchStatements1.ts, 236, 1))
>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 239, 10))
>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 239, 28))

function f35431(a: A) {
>f35431 : Symbol(f35431, Decl(exhaustiveSwitchStatements1.ts, 239, 43))
>a : Symbol(a, Decl(exhaustiveSwitchStatements1.ts, 241, 16))
>A : Symbol(A, Decl(exhaustiveSwitchStatements1.ts, 236, 1))

switch (a.kind) {
>a.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 239, 10), Decl(exhaustiveSwitchStatements1.ts, 239, 28))
>a : Symbol(a, Decl(exhaustiveSwitchStatements1.ts, 241, 16))
>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 239, 10), Decl(exhaustiveSwitchStatements1.ts, 239, 28))

case "abc":
case "def": return;
default:
a!.kind; // Error expected
>a : Symbol(a, Decl(exhaustiveSwitchStatements1.ts, 241, 16))
}
}
29 changes: 29 additions & 0 deletions tests/baselines/reference/exhaustiveSwitchStatements1.types
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,32 @@ function ff(o: O, k: K) {
>k : keyof O
}

// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };
>A : A
>kind : "abc"
>kind : "def"

function f35431(a: A) {
>f35431 : (a: A) => void
>a : A

switch (a.kind) {
>a.kind : "abc" | "def"
>a : A
>kind : "abc" | "def"

case "abc":
>"abc" : "abc"

case "def": return;
>"def" : "def"

default:
a!.kind; // Error expected
>a!.kind : any
>a! : never
>a : never
>kind : any
}
}
12 changes: 12 additions & 0 deletions tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,15 @@ function ff(o: O, k: K) {
k === 'c'; // Error
return o[k];
}

// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };

function f35431(a: A) {
switch (a.kind) {
case "abc":
case "def": return;
default:
a!.kind; // Error expected
}
}