How to exclude a key from an interface in TypeScript

后端 未结 4 504
既然无缘
既然无缘 2021-02-05 04:38

In TypeScript, you can combine two interface types like this

interface Foo {
    var1: string
}

interface Bar {
    var2: string
}

type Combined = Foo & Ba         


        
4条回答
  •  逝去的感伤
    2021-02-05 05:06

    You can't remove properties from already existing interfaces. Even trying to extend existing interface with the interface having value property set as optional will return an error.

    To avoid this issue, modify the typings of the target component so value property is optional.

    e.g.

    // ...
    class MyComponent extends React.Component<{value?: string}, State> {
    // ...
    }
    

    and then component produced when using High Order Function

    const valuedComponent = valueHOC(MyComponent);
    

    won't ask for value prop.

提交回复
热议问题