suggest `in` operator when accessing non-common property of union types
See original GitHub issue🔍 Search Terms
access to non-common property of union types, suggest in operator for union types
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
⭐ Suggestion
Background: https://twitter.com/kentcdodds/status/1420125238436655104?s=21
When accessing props that are not on all union types, tsc gives Property 'foo' does not exist on type 'XX'
, which is confusing to newcomers. We can suggest the in
operator when it is used inside if
condition.
📃 Motivating Example
type A = {one: string}
type B = {two: string}
type C = A | B
declare const thing: C
// 😕Before:
// Property 'two' does not exist on type 'C'.
// Property 'two' does not exist on type 'A'.(2339)
if (thing.two) {
// it's a B
}
// 😄After:
// Property 'two' does not exist on type 'C'.
// Property 'two' does not exist on type 'A'.(2339)
// Did you mean to use `'two' in thing`?
if (thing.two) {
// it's a B
}
💻 Use Cases
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Typescript property does not exist on union type - Stack Overflow
You have to narrow down the type. You can do so by using the in operator. const getText = (obj: Obj1 | Obj2):...
Read more >Suggestion: treat `in` operator as type guard which asserts ...
This suggestion narrows by adding properties to the type. In cases where y is not a union type or none of the constituents...
Read more >Handbook - Unions and Intersection Types - TypeScript
A union type describes a value that can be one of several types. We use the vertical bar ( | ) to separate...
Read more >C++ Core Guidelines - GitHub Pages
This document is a set of guidelines for using C++ well. ... The rules emphasize static type safety and resource safety.
Read more >Python 3.10: Cool New Features for You to Try
Deconstructing Data Structures; Using Different Kinds of Patterns; Matching Literal Patterns. Type Unions, Aliases, and Guards; Stricter Zipping of ...
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 FreeTop 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
Top GitHub Comments
Here’s what I would propose (not promising to take a PR on this, just my own opinion):
expr.prop === 'A'
”. This will seem like ✨magic✨ and people will love it, especially with a quickfix (??)constructor
function, suggest usinginstanceof
. Again this will be magicinstanceof
, we may as well give up and suggestin
, since there’s unlikely to be a better way to guard yourself in – many people would type assert here instead, which is obviously much more unsound thanin
I would also suggest suggesting “Try adding
two?: undefined
toA
” etc…This is sound and simple.