3 ms·
A bit click-baity title but as I read the article, I agree on the overall thesis. The lack of documentation on how to use types is really annoying when you star
by tekkk 4y ago
A bit click-baity title but as I read the article, I agree on the overall thesis. The lack of documentation on how to use types is really annoying when you start going deep utilizing generic types and whatnot and bumping into weird errors.
Like for example, I wanted to type a generic object you can persist to a localStorage. What's the correct type?
Can't use an interface if you make a recursive type so you need a type. But if you use a Record<string | number, Persistable> and your client has some regular interface APIResponse { foo: string, bar: AnotherObject } the types dont match. I forget the specifics, but I ended up using { [key in string | number]?: PersistableValues } where PersistableValues includes all the primitives as well as arrays, sets, maps etc which I serialize/deserialize on persist and hydrate. Using
`object` as type is listed as bad-practise in TS documentation which I agree but really, typing all the handled cases by hand is not fun either.
Wonder if I just should use any and throw an error if I encounter something I am unable to serialize.