TypeScript无法在子类方法中推断类型
所以我有一节TS课:TypeScript无法在子类方法中推断类型,typescript,tslint,tsc,Typescript,Tslint,Tsc,所以我有一节TS课: export abstract class CPComponent< P, S = RootState > extends React.Component<P, S> { cpComponentDidCatch?(p: P, s?: S, t?: this): void; constructor(p: P, s: any) { super(p, s); } abstract cpRender(p: P, s:
export abstract class CPComponent<
P,
S = RootState
> extends React.Component<P, S> {
cpComponentDidCatch?(p: P, s?: S, t?: this): void;
constructor(p: P, s: any) {
super(p, s);
}
abstract cpRender(p: P, s: S, t?: this): ReactElement;
componentDidCatch() {
return (
this.cpComponentDidCatch &&
this.cpComponentDidCatch(this.props, this.state, this)
);
}
render() {
return this.cpRender(this.props, this.state, this);
}
}
导出抽象类组件<
P
S=根状态
>扩展React.Component{
cpComponentDidCatch?(p:p,s?:s,t?:this):无效;
构造函数(p:p,s:any){
超级(p,s);
}
抽象cpRender(p:p,s:s,t?:this):元素;
componentDidCatch(){
返回(
这个.cpComponentDidCatch&&
this.cpComponentDidCatch(this.props,this.state,this)
);
}
render(){
返回this.cpRender(this.props,this.state,this);
}
}
然后我将其子类化为:
type State = {car: string};
type Props = {bug: string}
export class Foo extends CPComponent<Props,State> { // {bug: any},{car: any}
cpComponentDidCatch(p){
}
cpRender(){
return (
<div>
rendered
</div>
)
}
}
type State={car:string};
类型Props={bug:string}
导出类Foo扩展了CPComponent{//{bug:any},{car:any}
cpComponentDidCatch(p){
}
cpRender(){
返回(
提供
)
}
}
但问题是我得到了一个编译错误:
不应该知道p
的类型是p
//它想让我添加更多的文本,所以我们来看看(可能是被取代的),被抛弃的该死的sux,似乎是一个有用的特性