Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TechTalk - 2o22 - JS/ TS/ React mistakes and ho...

TechTalk - 2o22 - JS/ TS/ React mistakes and how to avoid them

There are a lot of things that can go wrong with the way we use JS/TS. Here, we will talk about some applicable best practices along the way and some widespread mistakes

Marko Arsić

October 13, 2022
Tweet

More Decks by Marko Arsić

Other Decks in Programming

Transcript

  1. HypeTech Tech education and shaping ideas into hype products hypetech.io

    | reactweek.dev Marko Arsić Founder and CEO @ HypeTech Founder of HypeTech Education Lecturer @ ReactWeek.dev Independent Tech Consultant Helping companies set up teams and standardize the development process github.com/marsicdev
  2. There are no exceptions here and each field is difficult

    and it seems rather hard to avoid mistakes at the very start.
  3. Some compare async/await with Promise and claim it is the

    next generation in the evolution of JavaScript asynchronous programming
  4. async/await IS an improvement, but it is no more than

    a syntactic sugar, which will not change our programming style completely.
  5. Async functions are still promises. You have to understand promises

    before you can use async functions correctly, most of the time you need to use promises along with async functions.
  6. Not Enabling Strict Mode Without TypeScript strict mode on, the

    typing can be too lax, and that will make our codebase less type-safe. It will give the wrong impression, as some think that by adding TypeScript all typing issues are automatically fixed.
  7. Redeclaring interfaces When typing component interfaces, it is common to

    need to have some different interfaces variants of the same type. Those can vary in one or two parameters. A common mistake is to manually redefine those variations. That will lead to: • unnecessary boilerplate. • multiple changes are needed if one property changes in one place, that change needs to be propagated to many files.
  8. Not Relying on Type Inference TypeScript inference is one of

    the most powerful tools of this programming language. It does all the work for us. We only have to make sure that all the pieces add together with as little intervention as possible. A crucial operator to achieving this is the typeof.
  9. Incorrect use of Overloading TypeScript supports overloading natively. That is

    great as it can improve the readability of our contracts. However, it is different from other typed overloading languages. There are scenarios where it might make our code more complex and verbose.
  10. Using the Function Type TypeScript ships with the Function type.

    It is like using the any keyword but for functions only. Enabling strict mode won’t prevent us from using it. Function type: • accepts any number and type of parameters. • the return type is always any
  11. Relying on a Third Party for Immutability TS provides all

    the necessary tooling to make sure we don’t mutate our objects. We don’t need to add heavy libraries like ImmutableJS to our codebase.
  12. These guidelines are simple to follow and are meant to

    help you embrace ecosystem rather than fight it.
  13. By applying these simple tips you will have a better,

    less verbose, and easy to maintain codebase.
  14. Q & A As everything good in life, knowledge is

    great only when shared https://discord.gg/94uhCAkFKf