React Props with TypeScript
Feb 26, 2021
一般使用 TypeScript 寫 React Component 可以先把 props 宣告成 interface,然後用 function 的方法寫成下面的樣子。
但上面的寫法中,如果使用時不是 <Child />
這種self close的寫法,就會錯誤。因為 ChildProps
並沒有 children
,但寫成 <Child></Child>
的模式,就一定會有 children
。
使用 React.FC<T>
來宣告成 Functional Component 就可以解決此問題。