1. Introduction TypeScript is not real when compared to the type systems found in languages like C# / Java for example, it has absolutely no power at runtime because it does not exist in the native code we send to the browser, so we must not write development code that skews logical inference as that… Continue reading TypeScript best practice guide
Category: Typescript
TypeScript: any vs unknown
Ideally we should never use any, but let’s say we are migrating a legacy project to TypeScript and some degree of pragmatism is in order. Let’s consider this block of code: to get the ball rolling with TypeScript someone probably added an any type here and it seems fairly safe as we never do anything… Continue reading TypeScript: any vs unknown
errors: dev time vs compile time vs runtime
When writing code one of our goals should always be to catch errors as early as possible. If our tools / code exist in a way that means we don’t catch all or most errors until runtime…then we have serious flaws in our approach. Take TypeScript for example. If we actually use this tool right… Continue reading errors: dev time vs compile time vs runtime
TypeScript – don’t misuse casting
When feeling like you want to solve a type error with forceful casting, consider using one of TypeScript’s utility types instead. And please never use this pattern – object as unknown as SomeType Casting like this takes away TypeScripts power because you are now telling it what to believe rather than the tooling basing that… Continue reading TypeScript – don’t misuse casting
Keep your FE dumb!
Try to keep your client as dumb as possible, to achieve this you should avoid the following: In many cases your service layer can take the heavy lift away from the FE which takes away complexity/ risk and avoids duplicating/ mutating business logic + domain sources of truth.
TypeScript – please use strict mode
Many adopters of TypeScript will use it without strict mode enabled, by doing this you are forfeiting some of the safety that TS can provide during development. Strict Null Checks are a great example of what you might be missing! If you have a legacy codebase and you are trying to phase TS in, checkout… Continue reading TypeScript – please use strict mode
React Hook – asyc request
Think of all the React components that can end up having a useEffect that will call some API which then updates some state to not only hold the value of that but also track request progress, if only there was a way we could abstract some of this away and have dumber components…well consider the… Continue reading React Hook – asyc request
TypeScript – browser Santa Clause
At the beginning of my career I didn’t always enjoy front end development because no matter where you turned Javascript was always lurking, and I didn’t really like it. The dynamic nature of Javascript always made me uneasy because I had been so used to Java. Thankfully the game has really changed, especially because of… Continue reading TypeScript – browser Santa Clause
TypeScript – avoid using “any”
In many Node/ React Typescript repos I have seen, the authors have opted to disable the popular linting rule that complains about using the any type. I don’t think this is a good idea, why is that? Because Typescript is just syntactic sugar I think there is sometimes a temptation to try to “get round… Continue reading TypeScript – avoid using “any”
Jest/TypeScript – partial type mock
Consider these types: Let’s say you are testing a component that fires off an API call to retrieve on object that should match IHuman. In your test you will want to create a mock version of this, – usually referred to as stub or a fixture. Let’s look at an example of how that might… Continue reading Jest/TypeScript – partial type mock