使用typescript2.8新的条件一般类型的特点,就是它可以提取TProps
一个的React.ComponentType<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
这是可能的,任何人都可以提供解决方案吗?