Journals for

2022-09-20

🔗

Got a spam text today addressed to a group of people, and the larger disruption than the initial text itself was everyone else angry and replying to the "sender." When of course the sender of the text was the only party not seeing it, since it was just a computer program. Oh well :)

Learning

Typescript Const Assertions

I keep forgetting this, so writing it down here. Typescript's const assertion declarations are helpful for telling Typescript that you aren't going to mutate an object and so it can more narrowly type the object.

// Typed as ['a', 'b'] instead of string[];
const values = ['a', 'b'] as const;
// Typed as [number, number] instead of number[];
const nums = [1, 2] as const;

This especially comes in handy when the values need to match up to keys in an object or you have a function argument of type [number, number] to ensure a certain array length.

Links


Thanks for reading! If you have any questions or comments, please send me a note on Twitter.