Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript interface optional properties depending on other property

Let's say we have the following Typescript interface:

interface Sample {
    key1: boolean;
    key2?: string;
    key3?: number;
};

In this case, key1 is always required, key2 is always optional, while key3 should exist if key1 is true and should not exist if key1 is false. In another word, a key's occurrence depends on another key's value. How can we achieve this in Typescript?

like image 759
Viv Avatar asked Jul 19 '18 00:07

Viv


People also ask

How do I specify optional properties in TypeScript?

In TypeScript, you can specify that some or all properties of an object are optional. To do so, just add a question mark (?) after the property name.

Can we have an interface with optional and default properties in TypeScript?

If you want to set the properties of an interface to have a default value of undefined , you can simply make the properties optional. Copied!

How do you override properties in interface TypeScript?

Use the Omit utility type to override the type of an interface property, e.g. interface SpecificLocation extends Omit<Location, 'address'> {address: newType} . The Omit utility type constructs a new type by removing the specified keys from the existing type.

What is TypeScript omit?

The TypeScript Omit utility type Like the Pick type, the Omit can be used to modify an existing interface or type. However, this one works the other way around. It will remove the fields you defined. We want to remove the id field from our user object when we want to create a user.


2 Answers

The most straightforward way to represent this is with a type alias instead of an interface:

type Sample = {
  key1: true,
  key2?: string,
  key3: number
} | {
  key1: false,
  key2?: string,
  key3?: never
}

In this case the type alias is the union of two types you're describing. So a Sample should be either the first constituent (where key1 is true and key3 is required) or the second constituent (where key1 is false and key3 is absent).

Type aliases are similar to interfaces but they are not completely interchangeable. If using a type alias leads to some kind of error, please add more detail about your use case in the question.

Hope that helps. Good luck!

like image 137
jcalz Avatar answered Oct 22 '22 11:10

jcalz


I also really like the way Ben Ilegbodu (https://www.benmvp.com/blog/conditional-react-props-typescript/) does it:

Give these three different cases in JSX:

<Text>not truncated</Text>
<Text truncate>truncated</Text>
<Text truncate showExpand>truncated w/ expand option</Text>

... Here is the corresponding TypeScript definition to make the showExpanded property required when the truncate prop is present:

interface CommonProps {
  children: React.ReactNode

  // ...other props that always exist
}

type TruncateProps =
  | { truncate?: false; showExpanded?: never }
  | { truncate: true; showExpanded?: boolean }

type Props = CommonProps & TruncateProps

const Text = ({ children, showExpanded, truncate }: Props) => {
  // Both truncate & showExpanded will be of
  // the type `boolean | undefined`
}```

like image 4
Mr Washington Avatar answered Oct 22 '22 09:10

Mr Washington