Hacker News new | ask | show | jobs
by spiffytech 1210 days ago
I think that's just a contrived example. It looks like it should work with types like `(string | null)[]`, where individual members are only known on runtime.

That's very appealing to be; I commonly .map() an array to a nullable type, then want to filter out the nulls.

1 comments

How could it help with anything at runtime? The type system only exists at compile time, right?
Yeah, it only exists at compile time, so in "real" code you won't know the type of e.g., array index 2 at compile-time, but you will know "each item in this array is either type A or type B".

You'll often want to work on only the A's in the array, and so you need to whittle down the data, but also inform TypeScript that you've done so. The return type on your .filter() callback tell TS what type the entire resulting array should be.

When filtering out nulls, you normally have to write a user-defined type guard everywhere you call .filter(), which is irksome, so this project configures the built-in Boolean() function to behave similarly when passed to .filter().