React/TypeScript: extending a component with additional properties

前端 未结 5 598
梦如初夏
梦如初夏 2021-02-01 16:05

I am trying to use react to recreate my currents components (written in pure typescript) but I can\'t find a way to give additional props to a component extending an other.

5条回答
  •  走了就别回头了
    2021-02-01 16:31

    For those who need, base classes can declare required/abstract methods that all instances must implement:

    import { Component } from 'react'
    
    
    abstract class TestComponent

    extends Component { abstract test(): string } type Props = { first: string, last: string, } type State = { fullName: string, } class MyTest extends TestComponent { constructor(props: Props) { super(props) this.state = { fullName: `${props.first} ${props.last}` } } test() { const { fullName } = this.state return fullName } }

提交回复
热议问题