I am creating a component which will be extended in future, for that i need to make it more generic.
I have a requirement something like below.
I have a class Base which accepts properties of type 'cell' and 'phone'.
I want to extend that in NewBase class and support new property newType.
The error I am getting is
[ts] This condition will always return 'false' since the types '"cell" | "phone"' and '0' have no overlap.
I am not getting enough ideas to extend the interface to support the new class Please Help...
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();
}
}
Component's signature for generics. If you peek in the typedef file you'll see it'sReact.Component<Props, State>. Also, Facebook recommends composition over inheritance