TypeScriptかなり初心者です。
tsx
1interface IStatham { 2 hasGun: boolean; 3} 4 5class Statham extends React.Component<IStatham> { 6 constructor(props: IStatham) { 7 super(props); 8 } 9 10 public componentDidUpdate(prevProps: IStatham) { 11 // 処理 12 } 13 14 public render() { 15 return this.props.hasGun ? <p>Statham has a gun.</p> : <p>Statham is bare hands.</p>; 16 } 17}
以上のようなコンポーネントがあった時、IStatham
をクラス内で共通の変数にして、他で使用する場合に代入したいです。つまり、以下のようなこと。
tsx
1interface IStatham { 2 hasGun: boolean; 3} 4 5class Statham extends React.Component<IStatham> { 6// React.Component<ここにある型定義> を、Sとしてクラス内で使用したい。 7 constructor(props: S) { 8 super(props); 9 } 10 11 public componentDidUpdate(prevProps: S) { 12 // 処理 13 } 14 15 public render() { 16 return this.props.hasGun ? <p>Statham has a gun.</p> : <p>Statham is bare hands.</p>; 17 } 18}
以上のようなことが可能であれば、例えばIStatham => IJason
に置き換えた時に、クラス内まで書き換える必要がなくなるので楽だなぁと。
こうした場合、どのようにしたらよいのでしょうか?
よろしくお願いします。
Sとしたときに、Sが"hasGun"を持つことはどこで検証する予定ですか?また、IStathamとIJsonの共通項は何かありますか?