|
|
|
|
|
by phailhaus
1713 days ago
|
|
`Record` is a dangerous type, and I would recommend against it. The problem is that it assumes any key is valid and will return a value type. Example: type Tips = Record<string, TipObject>
const tips: Tips = {}
tips["hello"] // TipObject, but you actually get undefined
It's better to define a Dictionary type like so: type Dictionary<K extends string | number | Symbol, V> = Partial<Record<K, V>>
Then to use: type Tips = Dictionary<string, TipObject>
const tips: Tips = {}
tips["hello"] // TipObject | undefined
That way, you're always forced to check for existence, and you never accidentally attempt to access properties on `undefined`. |
|
[1] https://www.typescriptlang.org/docs/handbook/2/objects.html#...
[2] https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAc...