How do I specify an empty interface as a generic argument in Typescript?

Viewed 817

I have a React Component in Typescript. This component has a State but no Properties. I have the following code:

interface State { ... }

class MyComponent extends React.Component<{}, State> { ... }

Today I moved from tslint to eslint, and got a warning, saying I should not use {} as a type.

When I created an empty interface:

interface Prop { }

And passed that as the first generic argument, I got another warning, saying an empty interface is the same as {}.

So, what should I do? I can't use never since it causes a whole bunch of other errors, please - it really doesn't make sense here. Is there any alternative to disabling the ban-types rule?

1 Answers

Assuming you want the component to be able to parent other components, you might want to accept the children key even if it doesn't have other props.

Your code would then look as below and wouldn't be passing an empty spec for props...

class MyComponent extends React.Component<{ children?: ReactNode }, State> { ... }
Related