Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Golden Rule of Generics #9

Open
danvk opened this issue Mar 1, 2022 · 7 comments
Open

The Golden Rule of Generics #9

danvk opened this issue Mar 1, 2022 · 7 comments
Labels
💬 blog comments Comments on effectivetypescript.com (Utterances)

Comments

@danvk
Copy link
Owner

danvk commented Mar 1, 2022

The Golden Rule of Generics

https://effectivetypescript.com/2020/08/12/generics-golden-rule/

@danvk danvk added the 💬 blog comments Comments on effectivetypescript.com (Utterances) label Mar 1, 2022
@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by Viacheslav Dobromyslov (viacheslavdobromyslov) on 2020-08-13 06:12:

Thank you for these examples. I want to add just one small note: els.map(el => '' + el) is not a good way to convert number to string. ESlint will mark it as an error.

@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by danvdk on 2020-08-14 10:38:

Are you referring to the no-implicit-coercions rule? The argument against '' + x for string coercion there is entirely stylistic. Error and "not a good way" seem a bit strong for a matter of style.

@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by Zackery Zing (zackeryzing) on 2020-08-14 02:59:

In my linting rules, I always force functions to have a return type. I think in an article like this it is useful to have return types on all functions so there is no missing information.

@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by danvdk on 2020-08-14 07:35:

What I’d really like is to have mouseover in the code snippets on the blog to see the inferred type, just like in your editor. I know some people (and some linters) insist on annotating all return types, but I do not.

@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by Romain Deneau (romain_deneau) on 2020-08-18 04:22:

Useful rule, nicely explained in this article 👍

I suggest to rename it "The Golden Rule of Generic Functions", because it's about functions, not all generic types - see basic counter example: in this generic type, type WithValue<t> = { value: T }, T appears only once.

@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by Vincent Jaubert (vincentjaubert) on 2021-05-16 05:14:

This is indeed pure gold, thank you.

@danvk
Copy link
Owner Author

danvk commented Mar 1, 2022

Comment by ThomasBurleson on 2021-12-07 11:32:

I really enjoyed reading this. Very important heuristics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💬 blog comments Comments on effectivetypescript.com (Utterances)
Projects
None yet
Development

No branches or pull requests

1 participant