Typescript: Conditional intersection type for react props

Created on 14 Feb 2019  路  2Comments  路  Source: microsoft/TypeScript

Hi, i think that ability to describe react component props as type with non required fields but it can be required if another field exists. For instance:

interface ComponentProps {
  sortable?: boolean
  onSortHandler - this field must be required only when sortable is true moreover it must don't exists in this interface
}

So, is it possible to do with conditional types ? For now i couldn't implement it

Most helpful comment

You don't need conditional types. Some like that should work:

type ComponentProps = {
  sortable: true,
  onSortHandler: any, // set the correct type
} | {
  sortable?: false | null,
};

NOTE: better ask questions to stackoverflow.

All 2 comments

You don't need conditional types. Some like that should work:

type ComponentProps = {
  sortable: true,
  onSortHandler: any, // set the correct type
} | {
  sortable?: false | null,
};

NOTE: better ask questions to stackoverflow.

@j-oliveras
Oh, sorry. Thank you very much.. My its my blame

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weswigham picture weswigham  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

manekinekko picture manekinekko  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

siddjain picture siddjain  路  3Comments