Say I have the following code:
function myFunction(x: { foo: number } | { bar: string }) {
How can I write some code to determine whether x is the first or second type?
Things I've considered:
- Writing an
x is MyTypefunction to check for afooproperty. Yes, I could do this, but it seems overkill for types that are only used as arguments to a single function. if ((x as { foo: number}).foo) { let y = x as { foo: number }. I could do this, but it defeats the point of a type system.- Give them both a common
typeproperty. Again, seems like overkill for types that are only used as arguments for one function.