Javascript 反应+;typescript继承和扩展属性
我正在创建一个将在将来扩展的组件,为此我需要使它更通用 我有如下要求 我有一个类基,它接受类型为“cell”和“phone”的属性 我想在NewBase类中扩展它,并支持新的属性newType 我得到的错误是 [ts]由于类型“cell”|“phone”和“0”没有重叠,因此此条件将始终返回“false” 我没有足够的想法来扩展接口以支持新类 请帮忙Javascript 反应+;typescript继承和扩展属性,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我正在创建一个将在将来扩展的组件,为此我需要使它更通用 我有如下要求 我有一个类基,它接受类型为“cell”和“phone”的属性 我想在NewBase类中扩展它,并支持新的属性newType 我得到的错误是 [ts]由于类型“cell”|“phone”和“0”没有重叠,因此此条件将始终返回“false” 我没有足够的想法来扩展接口以支持新类 请帮忙 export interface IBaseType { type: 'cell' | 'phone' } export class
export interface IBaseType {
type: 'cell' | 'phone'
}
export class Base extends React.Component<IBaseType > {
public render() {
const { name, type } = this.props;
return (() => {
switch (type) {
case 'cell': return <h1>Cell</h1>
case 'phone': return <h1>Phone</h1>
}
})()
}
}
interface NewType extends Omit<IBaseType, 'type'> {
type: 'newType' | IBaseType['type']
}
class NewBase extends Base {
constructor(props: NewType){}
public render(){
if(this.props.type === 'newType') {
return <h1>Hi i am new type</h1>
}
return super.render();
}
}
导出接口IBaseType{
键入:“手机”|“电话”
}
导出类基扩展React.Component{
公共渲染(){
const{name,type}=this.props;
返回(()=>{
开关(类型){
大小写“cell”:返回单元格
案例“电话”:返回电话
}
})()
}
}
接口NewType扩展忽略{
类型:“新类型”| IBaseType[“类型”]
}
类NewBase扩展了基{
构造函数(props:NewType){}
公共渲染(){
如果(this.props.type=='newType'){
返回嗨,我是新类型
}
返回super.render();
}
}
this.props的类型由传递给React.Component
的props type参数决定。在这种情况下,NewBase扩展Base
和Base扩展React.Component
,this.props
的类型仍然是IBaseType
,这就解释了错误。如果希望能够定义具有不同道具类型的Base
子类,则需要为道具类型向Base
添加类型参数。像这样的东西可能适合你:
interface ICommonType {
type: string;
}
export interface IBaseType {
type: 'cell' | 'phone'
}
export class Base<P extends ICommonType = IBaseType> extends React.Component<P> {
public render() {
const { name, type } = this.props;
return (() => {
switch (type) {
case 'cell': return <h1>Cell</h1>
case 'phone': return <h1>Phone</h1>
}
})()
}
}
interface NewType extends Omit<IBaseType, 'type'> {
type: 'newType' | IBaseType['type']
}
class NewBase extends Base<NewType> {
public render(){
if(this.props.type === 'newType') {
return <h1>Hi i am new type</h1>
}
return super.render();
}
}
接口类型{
类型:字符串;
}
导出接口IBaseType{
键入:“手机”|“电话”
}
导出类基扩展React.Component
{
公共渲染(){
const{name,type}=this.props;
返回(()=>{
开关(类型){
大小写“cell”:返回单元格
案例“电话”:返回电话
}
})()
}
}
接口NewType扩展忽略{
类型:“新类型”| IBaseType[“类型”]
}
类NewBase扩展了基{
公共渲染(){
如果(this.props.type=='newType'){
返回嗨,我是新类型
}
返回super.render();
}
}
this.props的类型由传递给React.Component
的props type参数决定。在这种情况下,NewBase扩展Base
和Base扩展React.Component
,this.props
的类型仍然是IBaseType
,这就解释了错误。如果希望能够定义具有不同道具类型的Base
子类,则需要为道具类型向Base
添加类型参数。像这样的东西可能适合你:
interface ICommonType {
type: string;
}
export interface IBaseType {
type: 'cell' | 'phone'
}
export class Base<P extends ICommonType = IBaseType> extends React.Component<P> {
public render() {
const { name, type } = this.props;
return (() => {
switch (type) {
case 'cell': return <h1>Cell</h1>
case 'phone': return <h1>Phone</h1>
}
})()
}
}
interface NewType extends Omit<IBaseType, 'type'> {
type: 'newType' | IBaseType['type']
}
class NewBase extends Base<NewType> {
public render(){
if(this.props.type === 'newType') {
return <h1>Hi i am new type</h1>
}
return super.render();
}
}
接口类型{
类型:字符串;
}
导出接口IBaseType{
键入:“手机”|“电话”
}
导出类基扩展React.Component
{
公共渲染(){
const{name,type}=this.props;
返回(()=>{
开关(类型){
大小写“cell”:返回单元格
案例“电话”:返回电话
}
})()
}
}
接口NewType扩展忽略{
类型:“新类型”| IBaseType[“类型”]
}
类NewBase扩展了基{
公共渲染(){
如果(this.props.type=='newType'){
返回嗨,我是新类型
}
返回super.render();
}
}
这不是组件的泛型签名。如果查看typedef文件,您将看到它是React.Component
。同样,Facebook也不是泛型的签名。如果查看typedef文件,您将看到它是React.Component
。还有Facebook