作者:谁的淡笑丶_227 | 来源:互联网 | 2023-02-01 22:48
1> Jokester..:
我能够在TS 2.3中做到这一点.关键是对该组件的"内部"和"外部"使用2种类型.
interface IProps {
value: V;
doSomething(val: V): void;
}
// type "inside" component
function _Comp(props: IProps) {
return ;
}
// type for "outside" of component
interface GenericsSFC extends React.SFC {
(props: IProps & { children?: React.ReactNode }, context?: any): JSX.Element;
}
const Comp = _Comp as GenericsSFC;
// dont type check: v is of type "hey"
v - 1} />;
现在还有更好的做法吗?Typescript在2.9中为JSX中的泛型添加了一些支持