1

I have the following case: two different interfaces (A, B) and one function which takes a parameter props as a conditional interface / union type. But I cannot use prop if it isn't declared in both interfaces.

Example:

interface A {
    name: string
};

interface B {
    age: number
};

function foo(props: A | B) {
    return props.name;
}

2 Answers 2

3

This is correct - you don't know if the name key exists on your props object.

You have two options:

1

function foo(props: A | B): string | undefined {
  if ('name' in props) {
    return props.name
  }
}

2.

interface A {
  name: string
  age?: undefined
}

interface B {
  name?: undefined
  age: number
}

function foo(props: A | B): string | undefined {
  return props.name
}

Why?

Typescript is correctly warning you because an object which does not have the name key is not the same as an object where the name key is undefined. Imagine this:

const a = {
  // name is missing
  age: 1
}

const b = {
  name: 'test',
  age: undefined
}

Object.keys(a) == ['age']
Object.keys(b) == ['name', 'age']

if ('age' in b) {
  console.log('this is true')
}

if ('name' in a) {
  throw new Error(`This is false`)
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could do it like this:

function foo(props: A | B) {
    if ((props as A).name) {
        return (props as A).name;
    }
    return undefined;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.