【发布时间】:2018-10-09 14:41:27
【问题描述】:
使用 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
这可能吗,有人可以提供解决方案吗?
【问题讨论】:
标签: reactjs typescript types conditional-types