-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Attempt to fix #9919 - Intersecting discriminated union breaks type narrowing #9939
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
161 changes: 161 additions & 0 deletions
161
tests/baselines/reference/discriminatedUnionTypesWithIntersection.errors.txt
This file contains hidden or 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,161 @@ | ||
tests/cases/conformance/types/union/discriminatedUnionTypesWithIntersection.ts(64,18): error TS2339: Property 'str' does not exist on type 'never'. | ||
tests/cases/conformance/types/union/discriminatedUnionTypesWithIntersection.ts(75,26): error TS2339: Property 'str' does not exist on type 'never'. | ||
tests/cases/conformance/types/union/discriminatedUnionTypesWithIntersection.ts(82,27): error TS2339: Property 'num' does not exist on type 'never'. | ||
tests/cases/conformance/types/union/discriminatedUnionTypesWithIntersection.ts(93,31): error TS2339: Property 'num' does not exist on type 'never'. | ||
tests/cases/conformance/types/union/discriminatedUnionTypesWithIntersection.ts(97,26): error TS2339: Property 'str' does not exist on type 'never'. | ||
|
||
|
||
==== tests/cases/conformance/types/union/discriminatedUnionTypesWithIntersection.ts (5 errors) ==== | ||
type WithX = { | ||
x: string; | ||
} | ||
|
||
type BoxedValue = { kind: 'int', num: number } | ||
| { kind: 'string', str: string } | ||
|
||
type BoxIntersection = BoxedValue & WithX | ||
|
||
type BoxInline = { kind: 'int', num: number } & WithX | ||
| { kind: 'string', str: string } & WithX | ||
|
||
function getValueAsString_inline_if(value: BoxInline): string { | ||
if (value.kind === 'int') { | ||
value; // { kind: "int", num: number } & { x: string } | ||
return '' + value.num; | ||
} | ||
|
||
value; // { kind: "string", str: string } & { x: string } | ||
return value.str; | ||
} | ||
|
||
function getValueAsString_inline_switch(value: BoxInline): string { | ||
switch (value.kind) { | ||
case 'int': | ||
value; // { kind: "int", num: number } & { x: string } | ||
return '' + value.num; | ||
|
||
case 'string': | ||
value; // { kind: "string", str: string } & { x: string } | ||
return value.str; | ||
} | ||
} | ||
|
||
function getValueAsString_if(value: BoxIntersection): string { | ||
if (value.kind === 'int') { | ||
value; // { kind: "int", num: number } & { x: string } | ||
return '' + value.num; | ||
} | ||
|
||
value; // { kind: "string", str: string } & { x: string } | ||
return value.str; | ||
} | ||
|
||
function getValueAsString_switch(value: BoxIntersection): string { | ||
switch (value.kind) { | ||
case 'int': | ||
value; // { kind: "int", num: number } & { x: string } | ||
return '' + value.num; | ||
|
||
case 'string': | ||
value; // { kind: "string", str: string } & { x: string } | ||
return value.str; | ||
} | ||
} | ||
|
||
function getValueAsString_if_fixed(value: BoxIntersection & { kind: 'int' }): string { | ||
if (value.kind === 'int') { | ||
value; // { kind: "int", num: number } & { x: string } & { kind: "number" } | ||
return '' + value.num; | ||
} | ||
|
||
value; // never | ||
return value.str; | ||
~~~ | ||
!!! error TS2339: Property 'str' does not exist on type 'never'. | ||
} | ||
|
||
function getValueAsString_switch_fixed(value: BoxIntersection & { kind: 'int' }): string { | ||
switch (value.kind) { | ||
case 'int': | ||
value; // { kind: "int", num: number } & { x: string } & { kind: "number" } | ||
return '' + value.num; | ||
|
||
case 'string': | ||
value; // never | ||
return value.str; | ||
~~~ | ||
!!! error TS2339: Property 'str' does not exist on type 'never'. | ||
} | ||
} | ||
|
||
function getValueAsString_if_never(value: BoxIntersection & { kind: number }): string { | ||
if (value.kind === 'int') { | ||
value; // never | ||
return '' + value.num; | ||
~~~ | ||
!!! error TS2339: Property 'num' does not exist on type 'never'. | ||
} | ||
|
||
value; // { kind: "string", str: string } & { x: string } & { kind: "number" } | ||
return value.str; | ||
} | ||
|
||
function getValueAsString_switch_never(value: BoxIntersection & { kind: number }): string { | ||
switch (value.kind) { | ||
case 'int': | ||
value; // never | ||
return '' + value.num; | ||
~~~ | ||
!!! error TS2339: Property 'num' does not exist on type 'never'. | ||
|
||
case 'string': | ||
value; // never | ||
return value.str; | ||
~~~ | ||
!!! error TS2339: Property 'str' does not exist on type 'never'. | ||
} | ||
} | ||
|
||
type Ext = { strVal: string } & { kind: 'int' } | ||
| { intVal: string } & { kind: 'string' } | ||
|
||
type Boxed2 = { kind: 'null' } | ||
| BoxIntersection & Ext; | ||
|
||
function arbitraryNesting_if(value: Boxed2): void { | ||
if (value.kind === 'null') { | ||
return; | ||
} | ||
|
||
if (value.kind === 'int') { | ||
value; // { kind: 'int', num: number } & { x: string; } | ||
value.x; | ||
value.num; | ||
value.strVal; | ||
return; | ||
} | ||
|
||
value; // { kind: 'string', str: string } & { x: string } & { intVal: string } & { kind: 'string' } | ||
value.x; | ||
value.str; | ||
value.intVal; | ||
} | ||
|
||
function arbitraryNesting_switch(value: Boxed2): void { | ||
switch (value.kind) { | ||
case 'null': | ||
return; | ||
|
||
case 'int': | ||
value.x; | ||
value.num; | ||
value.strVal; | ||
return; | ||
|
||
case 'string': | ||
value.x; | ||
value.str; | ||
value.intVal; | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't want to narrow if the original type was a union? That doesn't sound right.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, maybe the better way to write it is:
i.e. if neither the discrimination property is a string-literal-union, nor the parent type is a union or intersection. I guess I made it unintelligible by applying De Morgan's law.