posts from @cactus tagged #made with prechoster

also:

type Data1 = Record<string, number>;
const a1: Data1 = {x: 3};
console.log(a1["y"] + 4); // πŸ˜” typechecks but doesn't work!

type Data2 = Partial<Record<string, number>>;
const a2: Data2 = {x: 3};
console.log(a2["y"] + 4); // πŸ˜‡ correctly doesn't typecheck!
if ("x" in a2) {
    console.log(a2["x"] + 4); // πŸ˜” works but doesn't typecheck!
}
for (const k in a2) {
    console.log(a2[k] - 1); // πŸ˜” works but doesn't typecheck!
}

typescript playground

i would like to have a type that expresses the idea of "the key might not be present, but if it is, the value will absolutely be there" so that typescript can do typescript things, but unfortunately, it looks like typescript is incapable of expressing ideas like that.

sometimes typescript lets you do interesting things, and sometimes it tells you to fuck yourself. i have been writing typescript full time for 10 months and i still don't have a good sense for when to go fuck myself


Β