Hacker News new | ask | show | jobs
by _0o6v 1992 days ago
As the full lesson says:

For every soundness-related bug that sneaks through, TypeScript will probably save you from hundreds of "cannot read property of undefined" errors.

1 comments

In JavaScript its good practice to check function parameters for errors. TypeScript (TS) fans call this "poor mans type checker". It has however several benefits over TS. Like more simple then extended type declaration: if(arr.lenght==0) throw new Error("expected arr to be not empty")

Vs

type NonEmptyArray<T> = [T, ...T[]];

Or if you need to check if the array has exactly 3 items. How would you do that in TS?

> Or if you need to check if the array has exactly 3 items. How would you do that in TS?

Like that: https://www.typescriptlang.org/play?#code/C4TwDgpgBAKgFgJwhA...