Remove ComponentLifecycle interface in favour of individual interfaces
See original GitHub issueI’d like to propose removing the ComponentLifecycle interface and replace it with individual interfaces, e.g.
interface WillMount {
componentWillMount(): void;
}
interface WillReceiveProps<PropsType, ContextType> {
componentWillReceiveProps(nextProps: PropsType, nextContext: ContextType): void;
}
This way instead of Component implementing the ComponentLifecycle interface where everything is marked optional, the consumer would be able to explicitly implement WillMount, for example.
This allows for greater type safety as TypeScript will be able to know if the interface is implemented incorrectly, something it can’t do with the current solution.
This has the added benefit of allowing vscode (and presumably other editors) to auto implement the interfaces. (And I wouldn’t have to flick to the docs because I forgot the signature literally every time I need to implement a lifecycle event 😳)
I realise this would deviate from the React typings, but I am hoping that is not a major concern.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (9 by maintainers)
Top GitHub Comments
I agree that it would be a nice way to keep things in sync, though there would need to be a way to define extra types/interfaces either in jsdoc or in a separate .d.ts file.
There seems to be two existing solutions, jsdoc-typescript-plugin and tsd-jsdoc. I am unsure of benefits/downsides of either of them. I can probably play around with them sometime this week
(note to self, never mention issues in git commit messages, to prevent github spam).