使用TypeScript 2.8的新条件泛型类型功能,能否提取React.ComponentType<TProps>
组件的TProps
?我想要一个类型, 可以同时适用于ComponentType
和TProps
本身,所以您可以作为开发人员传递其中的任何一个:
例如:
interface TestProps {
foo: string;
}
const TestComponent extends React.Component<TestProps, {}> {
render() { return null; }
}
// now I need to create a type using conditional types so that
// I can pass either the component or the props and always get the
// TProps type back
type ExtractProps<TComponentOrTProps> = /* ?? how to do it? */
type a = ExtractProps<TestComponent> // type a should be TestProps
type b = ExtractProps<TestProps> // type b should also be TestProps
这是否可能,并且有人能够提供解决方案吗?