我想为 React props 扩展一个接口。但我收到 TS 错误错误:(19, 35) TS2304: 找不到名称 'T'。
1)为什么会出错?<T>
是泛型。不能在使用前声明。
2) 为什么 TS 在我的代码中抛出错误,但不会在此处抛出绝对类型的 React 类型定义。泛型类型<T>
和许多其他类型在其代码中随处可见。他们为什么不扔?
3)如何以正确的方式扩展它?
这是我的代码。
我从DefineTyped 为 React导入了接口props:
import React, {Props, PureComponent} from 'react';
// TS throws Error:(20, 27) TS2304: Cannot find name 'T'.
interface IHomeProps extends Props<T> { }
class Home extends PureComponent<IHomeProps> {
render() {
...
}
}
// interface Props definition
interface Props<T> {
children?: ReactNode;
key?: Key;
ref?: LegacyRef<T>;
}