by planede · 3y ago · 0 comments
const taskCounts = { work: 10, home: 5, personal: 3 };
This probably has the deduced type of `Record<"work" | "home" | "personal", number>`.I wonder if there is a hole in the type system that could be filled with an `ExactKeyTypeRecord<K, V>`, which is invariant in K and is a subtype of `Record<K, V>`. Maybe this could have Object.keys() defined as expected.
I have no experience with TS, but I believe Python's TypedDict has a bunch of similar holes in the type system that could be mitigated with a bit more elaborate type hierarchies.
const taskCounts = { work: 10, home: 5, personal: 3 };
const what : Record<string, number> = taskCounts; // OK, covariance?
const the : Record<"work" | "home", number> = taskCounts; // Also OK, contravariance?
const hell : Record<"work" | "home" | "personal" | "hello", number> = taskCounts; // This is an error
https://www.typescriptlang.org/play?#code/MYewdgzgLgBFCGEDWB...