question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove ComponentLifecycle interface in favour of individual interfaces

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jamesbirtlescommented, Apr 4, 2017

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

0reactions
marvinhagemeistercommented, Mar 4, 2018

(note to self, never mention issues in git commit messages, to prevent github spam).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular life-cycle events call without implementing interface?
The use of the Angular lifecycle interfaces is optional. ... misspelled the method name or the method was accidentally removed.
Read more >
Handling Lifecycles with Lifecycle-Aware Components
This interface abstracts the ownership of a Lifecycle from individual classes, such as Fragment and AppCompatActivity , and allows writing ...
Read more >
User Profile Component Interface to Delete Role from Mass ...
Hello, I am looking for information on a method to delete role(s) from a mass list of users. I do NOT want to...
Read more >
Lifecycle hooks - Angular
Respond to events in the lifecycle of a component or directive by implementing one or more of the lifecycle hook interfaces in the...
Read more >
React.Component
Each component has several “lifecycle methods” that you can override to run code ... This method is called when a component is being...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found