• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Property does not exist on conditional custom type

Resolved: Property does not exist on conditional custom type

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a type representing the object that gets passed in as a function’s parameters. I want the function to do different things based on if one property or the other property was in that object. Here’s an example:
However, I get a Typescript error when I try to destructure foo and bar: “Property foo does not exist on type FooOrBar.” (and the same for bar). Destructuring in the function parameters doesn’t help. What is the best way to rewrite this to satisfy TS?

Answer:

Typescript will throw an error because there can never be a case when both foo and bar are in the object. You cannot destructure a property which does not exist when using TypeScript.
Using the same union opertaor, you can use the in operator though:
Link

If you have better answer, please add a comment about this, thank you!

typescript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to compare two text files and and change the sign of the data in powershell?

27/03/2023

Resolved: Java Virtual Machines deleted

27/03/2023

Resolved: PyCharm cannot see my newly compiled .pyc see on import

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.