Question · JavaScript and React course · Module 12: React and TypeScript

Default Value for an Optional Prop

How do you set a default value for an optional prop defined as subtitle?: string in React with TypeScript?

Answer options

  • Using React.defaultProps with Partial<Props> type
  • Using destructuring with a default value: { subtitle = 'None' }: Props
  • Using default in the interface: subtitle: string = 'None'
  • Using the setDefault() method on the prop

You can check the correct answer by solving this task in the CodeWorlds game.