-
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.
Allow type narrowed by multi property access for discriminant/typeof/…
…truthiness add tests update tests fix remove useless code fix test reference
- Loading branch information
1 parent
a75f26e
commit 40521d7
Showing
56 changed files
with
8,357 additions
and
247 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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 |
---|---|---|
|
@@ -86,5 +86,5 @@ function foo2(x: A | B): any { | |
>x : B | ||
} | ||
x; // never | ||
>x : never | ||
>x : B | ||
} |
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
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
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
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
91 changes: 91 additions & 0 deletions
91
tests/baselines/reference/discriminatedUnionTypes3.errors.txt
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,91 @@ | ||
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS2339: Property 's' does not exist on type 'T'. | ||
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(78,10): error TS2339: Property 'property' does not exist on type 'SomeReturnType'. | ||
Property 'property' does not exist on type 'Err'. | ||
|
||
|
||
==== tests/cases/conformance/types/union/discriminatedUnionTypes3.ts (2 errors) ==== | ||
type A = { type2: "a", a: number } | ||
type B = { type2: "b", b: number } | ||
type C = { type2: "c", b: number | string } | ||
|
||
type X = { type1: A, x: string } | ||
type Y = { type1: B, y: string } | ||
type Z = { type1: C, z: string } | ||
type W = { type1: undefined } | ||
|
||
function f32(x: X | Y) { | ||
switch (x.type1.type2) { | ||
case "a": | ||
x.x // typeof x is X | ||
break; | ||
case "b": | ||
x.y // typeof x is Y | ||
break; | ||
} | ||
} | ||
|
||
function f33(x: A | B) { | ||
switch (x.type2) { | ||
case "a": | ||
x // typeof x is X | ||
break; | ||
case "b": | ||
x // typeof x is Y | ||
break; | ||
} | ||
} | ||
|
||
function f34(x: X | Y) { | ||
if (x.type1.type2 === "a") { | ||
x.x // typeof x is X | ||
} else if (x.type1.type2 === "b") { | ||
x.y // typeof x is Y | ||
} | ||
} | ||
|
||
function f35(x: X | W) { | ||
if (x.type1?.type2 === "a") { | ||
x.x | ||
} | ||
} | ||
|
||
function f36(x: X | W) { | ||
x.type1?.type2 ?? x; | ||
} | ||
|
||
type S = { sub: { type0: X }, s: string } | ||
type T = { sub: { type0: Y }, t: string } | ||
|
||
function f37(s: S | T) { | ||
if (s.sub.type0.type1.type2 === "a") { | ||
s.s // typeof s is S | ||
s.sub.type0.x // type of s.sub.type is X | ||
s.sub.type0.type1.a // type of s.sub.type.type is A | ||
} else { | ||
s.s // type error! | ||
~ | ||
!!! error TS2339: Property 's' does not exist on type 'T'. | ||
} | ||
} | ||
|
||
// Repro from #44435 | ||
|
||
type Correct = { | ||
code: string | ||
property: true | ||
err: undefined | ||
} | ||
type Err = { | ||
err: `${string} is wrong!` | ||
} | ||
type SomeReturnType = Correct | Err; | ||
|
||
const example: SomeReturnType = {} as SomeReturnType; | ||
|
||
if (example.err === undefined) { | ||
example.property; // true | ||
~~~~~~~~ | ||
!!! error TS2339: Property 'property' does not exist on type 'SomeReturnType'. | ||
!!! error TS2339: Property 'property' does not exist on type 'Err'. | ||
} | ||
|
Oops, something went wrong.