Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - make an optional property required when another property is present

I have proptypes shown below

interface Props {
resource: string;
create?: boolean;
route?: string;
}

As can be seen above, create and route are optional props. However, I would like to implement the types such that if create prop is present then route must now be required. Anyone have any ideas how to do this?

like image 503
Yoofi Brown-Pobee Avatar asked Nov 02 '20 16:11

Yoofi Brown-Pobee


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 omit a property in TypeScript?

Use the Omit utility type to exclude a property from a type, e.g. type WithoutCountry = Omit<Person, 'country'> . The Omit utility type constructs a new type by removing the specified keys from the existing type. Copied!

Does TypeScript have optional?

TypeScript provides a Optional parameters feature. By using Optional parameters featuers, we can declare some paramters in the function optional, so that client need not required to pass value to optional parameters.

How do I pick a specific set of properties from TypeScript?

To pick a specific set of properties from an existing TypeScript type, and make them optional, you can do any of the following: Create a Custom Utility Type.

How do you transform a type with required and optional properties?

We can use this to transform a type with a mix of required and optional properties into a new type where all the properties are required but some of them may be undefined: This works by mapping over Required<T>, a version of our original type where all the optional properties have been replaced by required ones.

How to create a partial utility type in typescript?

Create a Custom Utility Type. Since the Partial utility type in TypeScript does not allow selection of specific set of properties, you can simply combine it with the Pick utility type, which would have the following syntax: This would select " bar " and " qux " from " Foo ", and make them optional in the new type.

How to make a parameter optional in typescript?

The optional parameters must appear after the required parameters in the parameter list. For example, if you make the b parameter optional, and c parameter required the TypeScript compiler will issue an error: error TS1016: A required parameter cannot follow an optional parameter. Use the parameter?: type syntax to make a parameter optional.


Video Answer


2 Answers

You should group these props and make that grouped prop optional as

interface RouteProps{
create: boolean;
route: string;
}

interface Props {
resource: string;
routeInfo?: RouteProps;
}
like image 88
DevLoverUmar Avatar answered Oct 06 '22 20:10

DevLoverUmar


You can split it into a union of two types and use create with a literal type:

type Props = {
  resource: string;
  create?: false;
  route?: string
} | {
  resource: string;
  create: true;
  route: string;
}
like image 21
Ingo Bürk Avatar answered Oct 06 '22 18:10

Ingo Bürk