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: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 bothfoo
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:If you have better answer, please add a comment about this, thank you!