-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed an issue with
missingType
narrowing by case undefined
in de…
…fault cases (#58001)
- Loading branch information
Showing
10 changed files
with
837 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ntUndefinedCase1(exactoptionalpropertytypes=false,nouncheckedindexedaccess=false).symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//// [tests/cases/compiler/narrowBySwitchDiscriminantUndefinedCase1.ts] //// | ||
|
||
=== narrowBySwitchDiscriminantUndefinedCase1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/57999 | ||
|
||
interface A { | ||
>A : Symbol(A, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 0, 0)) | ||
|
||
optionalProp?: "hello"; | ||
>optionalProp : Symbol(A.optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 2, 13)) | ||
} | ||
|
||
function func(arg: A) { | ||
>func : Symbol(func, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 4, 1)) | ||
>arg : Symbol(arg, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 6, 14)) | ||
>A : Symbol(A, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 0, 0)) | ||
|
||
const { optionalProp } = arg; | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 7, 9)) | ||
>arg : Symbol(arg, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 6, 14)) | ||
|
||
switch (optionalProp) { | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 7, 9)) | ||
|
||
case undefined: | ||
>undefined : Symbol(undefined) | ||
|
||
return undefined; | ||
>undefined : Symbol(undefined) | ||
|
||
case "hello": | ||
return "hello"; | ||
default: | ||
assertUnreachable(optionalProp); | ||
>assertUnreachable : Symbol(assertUnreachable, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 30, 1)) | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 7, 9)) | ||
} | ||
} | ||
|
||
function func2() { | ||
>func2 : Symbol(func2, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 17, 1)) | ||
|
||
const optionalProp = ["hello" as const][Math.random()]; | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 20, 7)) | ||
>const : Symbol(const) | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
switch (optionalProp) { | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 20, 7)) | ||
|
||
case undefined: | ||
>undefined : Symbol(undefined) | ||
|
||
return undefined; | ||
>undefined : Symbol(undefined) | ||
|
||
case "hello": | ||
return "hello"; | ||
default: | ||
assertUnreachable(optionalProp); | ||
>assertUnreachable : Symbol(assertUnreachable, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 30, 1)) | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 20, 7)) | ||
} | ||
} | ||
|
||
function assertUnreachable(_: never): never { | ||
>assertUnreachable : Symbol(assertUnreachable, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 30, 1)) | ||
>_ : Symbol(_, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 32, 27)) | ||
|
||
throw new Error("Unreachable path taken"); | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
} | ||
|
124 changes: 124 additions & 0 deletions
124
...nantUndefinedCase1(exactoptionalpropertytypes=false,nouncheckedindexedaccess=false).types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//// [tests/cases/compiler/narrowBySwitchDiscriminantUndefinedCase1.ts] //// | ||
|
||
=== narrowBySwitchDiscriminantUndefinedCase1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/57999 | ||
|
||
interface A { | ||
optionalProp?: "hello"; | ||
>optionalProp : "hello" | undefined | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
} | ||
|
||
function func(arg: A) { | ||
>func : (arg: A) => "hello" | undefined | ||
> : ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>arg : A | ||
> : ^ | ||
|
||
const { optionalProp } = arg; | ||
>optionalProp : "hello" | undefined | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
>arg : A | ||
> : ^ | ||
|
||
switch (optionalProp) { | ||
>optionalProp : "hello" | undefined | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
|
||
case undefined: | ||
>undefined : undefined | ||
> : ^^^^^^^^^ | ||
|
||
return undefined; | ||
>undefined : undefined | ||
> : ^^^^^^^^^ | ||
|
||
case "hello": | ||
>"hello" : "hello" | ||
> : ^^^^^^^ | ||
|
||
return "hello"; | ||
>"hello" : "hello" | ||
> : ^^^^^^^ | ||
|
||
default: | ||
assertUnreachable(optionalProp); | ||
>assertUnreachable(optionalProp) : never | ||
> : ^^^^^ | ||
>assertUnreachable : (_: never) => never | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
>optionalProp : never | ||
> : ^^^^^ | ||
} | ||
} | ||
|
||
function func2() { | ||
>func2 : () => "hello" | undefined | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
const optionalProp = ["hello" as const][Math.random()]; | ||
>optionalProp : "hello" | ||
> : ^^^^^^^ | ||
>["hello" as const][Math.random()] : "hello" | ||
> : ^^^^^^^ | ||
>["hello" as const] : "hello"[] | ||
> : ^^^^^^^^^ | ||
>"hello" as const : "hello" | ||
> : ^^^^^^^ | ||
>"hello" : "hello" | ||
> : ^^^^^^^ | ||
>Math.random() : number | ||
> : ^^^^^^ | ||
>Math.random : () => number | ||
> : ^^^^^^^^^^^^ | ||
>Math : Math | ||
> : ^^^^ | ||
>random : () => number | ||
> : ^^^^^^^^^^^^ | ||
|
||
switch (optionalProp) { | ||
>optionalProp : "hello" | ||
> : ^^^^^^^ | ||
|
||
case undefined: | ||
>undefined : undefined | ||
> : ^^^^^^^^^ | ||
|
||
return undefined; | ||
>undefined : undefined | ||
> : ^^^^^^^^^ | ||
|
||
case "hello": | ||
>"hello" : "hello" | ||
> : ^^^^^^^ | ||
|
||
return "hello"; | ||
>"hello" : "hello" | ||
> : ^^^^^^^ | ||
|
||
default: | ||
assertUnreachable(optionalProp); | ||
>assertUnreachable(optionalProp) : never | ||
> : ^^^^^ | ||
>assertUnreachable : (_: never) => never | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
>optionalProp : never | ||
> : ^^^^^ | ||
} | ||
} | ||
|
||
function assertUnreachable(_: never): never { | ||
>assertUnreachable : (_: never) => never | ||
> : ^^^^ ^^^^^ | ||
>_ : never | ||
> : ^^^^^ | ||
|
||
throw new Error("Unreachable path taken"); | ||
>new Error("Unreachable path taken") : Error | ||
> : ^^^^^ | ||
>Error : ErrorConstructor | ||
> : ^^^^^^^^^^^^^^^^ | ||
>"Unreachable path taken" : "Unreachable path taken" | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
...antUndefinedCase1(exactoptionalpropertytypes=false,nouncheckedindexedaccess=true).symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//// [tests/cases/compiler/narrowBySwitchDiscriminantUndefinedCase1.ts] //// | ||
|
||
=== narrowBySwitchDiscriminantUndefinedCase1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/57999 | ||
|
||
interface A { | ||
>A : Symbol(A, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 0, 0)) | ||
|
||
optionalProp?: "hello"; | ||
>optionalProp : Symbol(A.optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 2, 13)) | ||
} | ||
|
||
function func(arg: A) { | ||
>func : Symbol(func, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 4, 1)) | ||
>arg : Symbol(arg, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 6, 14)) | ||
>A : Symbol(A, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 0, 0)) | ||
|
||
const { optionalProp } = arg; | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 7, 9)) | ||
>arg : Symbol(arg, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 6, 14)) | ||
|
||
switch (optionalProp) { | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 7, 9)) | ||
|
||
case undefined: | ||
>undefined : Symbol(undefined) | ||
|
||
return undefined; | ||
>undefined : Symbol(undefined) | ||
|
||
case "hello": | ||
return "hello"; | ||
default: | ||
assertUnreachable(optionalProp); | ||
>assertUnreachable : Symbol(assertUnreachable, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 30, 1)) | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 7, 9)) | ||
} | ||
} | ||
|
||
function func2() { | ||
>func2 : Symbol(func2, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 17, 1)) | ||
|
||
const optionalProp = ["hello" as const][Math.random()]; | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 20, 7)) | ||
>const : Symbol(const) | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
switch (optionalProp) { | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 20, 7)) | ||
|
||
case undefined: | ||
>undefined : Symbol(undefined) | ||
|
||
return undefined; | ||
>undefined : Symbol(undefined) | ||
|
||
case "hello": | ||
return "hello"; | ||
default: | ||
assertUnreachable(optionalProp); | ||
>assertUnreachable : Symbol(assertUnreachable, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 30, 1)) | ||
>optionalProp : Symbol(optionalProp, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 20, 7)) | ||
} | ||
} | ||
|
||
function assertUnreachable(_: never): never { | ||
>assertUnreachable : Symbol(assertUnreachable, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 30, 1)) | ||
>_ : Symbol(_, Decl(narrowBySwitchDiscriminantUndefinedCase1.ts, 32, 27)) | ||
|
||
throw new Error("Unreachable path taken"); | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
} | ||
|
Oops, something went wrong.