As a newcomer to TypeScript, I am still learning a lot and came across this code snippet that involves the Record utility types, which is quite perplexing for me.
The code functions correctly in the playground environment.
const data = async (name: string, id: number): Promise<Record<number,string>> => {
const obj = {
foo: 'bar', //I'm confused why this works even though it's 'number'
}
return obj
}
However, the following code does not work (2nd line).
const foo1: Record<string, string> = { foo: 'bar' }
const foo2: Record<number, string> = { foo: 'bar' }
I am unsure about the correct type of data that should be used for the 'key'. Feel free to experiment on TypeScript Playground.