Re:Learn how to unleash the full potential of the type syste...
by ttymck on Tuesday September 20, 2022 @05:25PM
(#32918010)
Why Record and not object?
[ Parent
| on HN ]
Re:Learn how to unleash the full potential of the type syste...
by davidatbu on Tuesday September 20, 2022 @05:37PM
(#32918132)
Avoid the Object and {} types, as they mean 'any non-nullish value'.
This is a point of confusion for many developers, who think it means 'any object type'.
https://github.com/typescript-eslint/typescript-eslint/blob/...
Re:Learn how to unleash the full potential of the type syste...
by lediur on Tuesday September 20, 2022 @05:35PM
(#32918115)
Linters for TypeScript recommend using `Record<string, any>` instead of `object`, since using the `object` type is misleading and can make it harder to use as intended.
See:
- https://typescript-eslint.io/rules/ban-types/
- https://github.com/typescript-eslint/typescript-eslint/issue...
Re:Learn how to unleash the full potential of the type syste...
by KrishnaShripad on Tuesday September 20, 2022 @05:41PM
(#32918178)
Because you only want to merge two objects that have keys with string type. "object" is represented as Record<any, any>. That would mean, you can use any type as key. Here is an example:
function merge<
A extends object,
B extends object
>(a: A, b: B): A & B {
return { ...a, ...b }
}
const result = merge(() => {}, () => {}) // should fail!
const anotherResult = merge([1, 2], [3, 4]) // should fail!
Which is obviously not what you want.This table here gives you a good overview of differences between object and Record<string, unknown>: https://www.reddit.com/r/typescript/comments/tq3m4f/the_diff...