Union with non-distinct discriminating property breaks inference somewhat (confusing error messages, invalid intellisense)
See original GitHub issueTypeScript Version: 4.0.2
Search Terms: discriminated union, error messages
Code
type Action = {
type: 'action1';
payload: {
property1: string;
};
} | {
type: 'action1';
payload: {
property2: number;
};
} | {
type: 'action2';
payload: {
property3: boolean;
};
}
const action: Action = { // Actual error is here
type: 'action1',
payload: {
property3: true // Expected error here
}
}
Expected behavior:
Expected error at action.payload.property3
declaration, perhaps something like:
Object literal may only specify known properties, and 'property3' does not exist in type '{ property1: string; } | { property2: number; }'.
Also, once the object has been refined by the type action1
, intellisense should only provide property1
and property2
as a suggestion when declaring the payload
.
Actual behavior:
An error message at the first line of the declaration:
Type '{ type: "action1"; payload: { property3: true; }; }' is not assignable to type 'Action'.
Type '{ type: "action1"; payload: { property3: true; }; }' is not assignable to type '{ type: "action2"; payload: { property3: boolean; }; }'.o
Type '"action1"' is not assignable to type '"action2"'.(2322)
Also, once the object has been refined by the type action1
, intellisense still provides property3
as a suggestion when declaring the payload
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Confusing error messages, invalid intellisense and no auto ...
So the messages on line 3 & 4 state that 'bundle' property has the invalid value 'someBadToken' and that it is not assignable...
Read more >How can I get TypeScript IntelliSense to suggest valid ...
Union with non-distinct discriminating property breaks inference somewhat (confusing error messages, invalid intellisense.
Read more >Fixing Visual Studio Intellisense Errors - Rick Strahl's Web Log
I ran into a problem in one of my applications where Visual Studio was showing errors that were clearly not actual errors.
Read more >MSDN Documentation Errors and Omissions - Flounder
This summarizes a number of errors and omissions in the MSDN documentation. ... In the Remarks section, the error code AVI_BADFORMAT should be...
Read more >Viewing online file analysis results for 'JVC_82096.vbs'
Submit malware for free analysis with Falcon Sandbox and Hybrid Analysis technology. Hybrid Analysis develops and licenses analysis tools to fight malware.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think it is similar to this https://github.com/microsoft/TypeScript/issues/39438 issue related to contextual type in a completion list
The completions bit does seem like a bug, and I 100% agree with your logic about the error message, though I’m not super optimistic that it can easily be generalized. Worth taking a look, though. (cc @DanielRosenwasser for thoughts on error elaboration)