-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Disallow partial matches for discriminant properties when generating error messages #37589
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
weswigham
merged 1 commit into
microsoft:master
from
weswigham:sometimes-disable-partial-discriminants
Apr 1, 2020
Merged
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/unionErrorMessageOnMatchingDiscriminant.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,28 @@ | ||
tests/cases/compiler/unionErrorMessageOnMatchingDiscriminant.ts(21,5): error TS2322: Type 'null' is not assignable to type '{ a: string; }'. | ||
|
||
|
||
==== tests/cases/compiler/unionErrorMessageOnMatchingDiscriminant.ts (1 errors) ==== | ||
type A = { | ||
type: 'a', | ||
data: { a: string } | ||
}; | ||
|
||
type B = { | ||
type: 'b', | ||
data: null | ||
}; | ||
|
||
type C = { | ||
type: 'c', | ||
payload: string | ||
}; | ||
|
||
type Union = A | B | C; | ||
|
||
// error | ||
const foo: Union = { | ||
type: 'a', | ||
data: null | ||
~~~~ | ||
!!! error TS2322: Type 'null' is not assignable to type '{ a: string; }'. | ||
}; |
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/unionErrorMessageOnMatchingDiscriminant.js
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,31 @@ | ||
//// [unionErrorMessageOnMatchingDiscriminant.ts] | ||
type A = { | ||
type: 'a', | ||
data: { a: string } | ||
}; | ||
|
||
type B = { | ||
type: 'b', | ||
data: null | ||
}; | ||
|
||
type C = { | ||
type: 'c', | ||
payload: string | ||
}; | ||
|
||
type Union = A | B | C; | ||
|
||
// error | ||
const foo: Union = { | ||
type: 'a', | ||
data: null | ||
}; | ||
|
||
//// [unionErrorMessageOnMatchingDiscriminant.js] | ||
"use strict"; | ||
// error | ||
var foo = { | ||
type: 'a', | ||
data: null | ||
}; |
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/unionErrorMessageOnMatchingDiscriminant.symbols
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,53 @@ | ||
=== tests/cases/compiler/unionErrorMessageOnMatchingDiscriminant.ts === | ||
type A = { | ||
>A : Symbol(A, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 0, 0)) | ||
|
||
type: 'a', | ||
>type : Symbol(type, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 0, 10)) | ||
|
||
data: { a: string } | ||
>data : Symbol(data, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 1, 14)) | ||
>a : Symbol(a, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 2, 11)) | ||
|
||
}; | ||
|
||
type B = { | ||
>B : Symbol(B, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 3, 2)) | ||
|
||
type: 'b', | ||
>type : Symbol(type, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 5, 10)) | ||
|
||
data: null | ||
>data : Symbol(data, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 6, 14)) | ||
|
||
}; | ||
|
||
type C = { | ||
>C : Symbol(C, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 8, 2)) | ||
|
||
type: 'c', | ||
>type : Symbol(type, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 10, 10)) | ||
|
||
payload: string | ||
>payload : Symbol(payload, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 11, 14)) | ||
|
||
}; | ||
|
||
type Union = A | B | C; | ||
>Union : Symbol(Union, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 13, 2)) | ||
>A : Symbol(A, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 0, 0)) | ||
>B : Symbol(B, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 3, 2)) | ||
>C : Symbol(C, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 8, 2)) | ||
|
||
// error | ||
const foo: Union = { | ||
>foo : Symbol(foo, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 18, 5)) | ||
>Union : Symbol(Union, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 13, 2)) | ||
|
||
type: 'a', | ||
>type : Symbol(type, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 18, 20)) | ||
|
||
data: null | ||
>data : Symbol(data, Decl(unionErrorMessageOnMatchingDiscriminant.ts, 19, 14)) | ||
|
||
}; |
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/unionErrorMessageOnMatchingDiscriminant.types
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,53 @@ | ||
=== tests/cases/compiler/unionErrorMessageOnMatchingDiscriminant.ts === | ||
type A = { | ||
>A : A | ||
|
||
type: 'a', | ||
>type : "a" | ||
|
||
data: { a: string } | ||
>data : { a: string; } | ||
>a : string | ||
|
||
}; | ||
|
||
type B = { | ||
>B : B | ||
|
||
type: 'b', | ||
>type : "b" | ||
|
||
data: null | ||
>data : null | ||
>null : null | ||
|
||
}; | ||
|
||
type C = { | ||
>C : C | ||
|
||
type: 'c', | ||
>type : "c" | ||
|
||
payload: string | ||
>payload : string | ||
|
||
}; | ||
|
||
type Union = A | B | C; | ||
>Union : Union | ||
|
||
// error | ||
const foo: Union = { | ||
>foo : Union | ||
>{ type: 'a', data: null} : { type: "a"; data: null; } | ||
|
||
type: 'a', | ||
>type : "a" | ||
>'a' : "a" | ||
|
||
data: null | ||
>data : null | ||
>null : null | ||
|
||
}; |
23 changes: 23 additions & 0 deletions
23
tests/cases/compiler/unionErrorMessageOnMatchingDiscriminant.ts
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,23 @@ | ||
// @strict: true | ||
type A = { | ||
type: 'a', | ||
data: { a: string } | ||
}; | ||
|
||
type B = { | ||
type: 'b', | ||
data: null | ||
}; | ||
|
||
type C = { | ||
type: 'c', | ||
payload: string | ||
}; | ||
|
||
type Union = A | B | C; | ||
|
||
// error | ||
const foo: Union = { | ||
type: 'a', | ||
data: null | ||
}; |
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.
When do you not want to
skipPartial
discriminants?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.
So we report excess property errors on something like
a
andb
are both partial discriminants, but taken together, can discriminate to the first member. We have a test expecting this.