LoginSignup
1
0

More than 3 years have passed since last update.

【React+TypeScript】Function Componentで省略可能propsの省略した場合の値を型安全に指定する

Last updated at Posted at 2019-07-12

こんな問題がありますが、結論としては、回避策として「名前のついた型を指定しない」のが楽です。

リンク先から引用。

const MyComponent = (props: { name: string, telephone: string }) => {
  ...
}

MyComponent.defaultProps = { telephone: "222-333-4444" }

// Works - good
const test = <MyComponent name="Hulk Hogan" />

// Doesn't work - missing name, good
const test = <MyComponent />
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0