Javascript React和Flowtype-继承类

Javascript React和Flowtype-继承类,javascript,reactjs,typescript,flowtype,Javascript,Reactjs,Typescript,Flowtype,假设我有 // Foo.js type PropsType = { cool: boolean }; class Foo extends React.Component<PropsType> {} // Bar.js import Foo from './Foo'; type PropsBar = { temp: string }; class Bar extends Foo { test() { this.props.cool; // there is no er

假设我有

// Foo.js
type PropsType = { cool: boolean };
class Foo extends React.Component<PropsType> {}

// Bar.js
import Foo from './Foo';

type PropsBar = { temp: string };
class Bar extends Foo {
  test() {
     this.props.cool; // there is no error
     this.props.temp;
                ^^^^ Property not found in object type  
  }
}
//Foo.js
类型PropsType={cool:boolean};
类Foo扩展了React.Component{}
//Bar.js
从“/Foo”导入Foo;
类型PropsBar={temp:string};
类栏扩展了Foo{
测试(){
this.props.cool;//没有错误
这个.props.temp;
^^^^在对象类型中找不到属性
}
}

我的问题是,如何将额外的
道具
传递到
条形图
组件?

您需要使您的超级类通用化。正如
React.Component
是泛型的一样,您的类和函数也可以是泛型的

通过引入类型参数,可以使类或函数等声明成为泛型

让我们把
Foo
generic

export default class Foo<T> extends React.Component<FooProps & T> {}
如果您希望为不添加额外道具的
Foo
消费者保持简单性,可以为
T
指定默认类型,如中所示

export default class Foo<T = {}> extends React.Component<FooProps & T> {}

export class Bar extends Foo {}
导出默认类Foo扩展React.Component{}
导出类栏扩展了Foo{}

注意:以上所有语法在Flow和TypeScript中都有效。

Perfect!谢谢你的解释:)很高兴我能帮忙
export default class Foo<T = {}> extends React.Component<FooProps & T> {}

export class Bar extends Foo {}