question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
RyanCavanaughcommented, Aug 5, 2021

Here’s what I would propose (not promising to take a PR on this, just my own opinion):

  • If there’s a discriminant (we have logic that can detect this), suggest “Check for expr.prop === 'A'”. This will seem like ✨magic✨ and people will love it, especially with a quickfix (??)
  • If there’s not a discriminant, but the property is sourced from a type that also has a constructor function, suggest using instanceof. Again this will be magic
  • If there’s not a discriminant and there’s no plausible instanceof, we may as well give up and suggest in, 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 than in
0reactions
simonbuchancommented, Jul 19, 2022

I would also suggest suggesting “Try adding two?: undefined to A” etc…

This is sound and simple.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found