DEV Community

Discussion on: 5 TypeScript features that will improve your codebase

Collapse
 
nekitk profile image
Golubov Nikita

In the type guard example truck with load of 0 will be considered as F1 because 0 is falsy. It is better to check using in operator:

function carIsTruck(car: Truck | F1): car is Truck {
  return 'load' in car;
}

Such check can be used even without writing type guard function:
typescriptlang.org/docs/handbook/a...

Collapse
 
glebirovich profile image
Gleb Irovich

Good catch regarding the load, I will update the example.
You are also right, that we can use it inline, but the idea was also to discuss the type guards. If this check is extracted it can be reused across the app if necessary and it also improves readability of the code.