Values
will automatically be assigned the type of an array containing values returned by the map function. So in most cases, there is no need to explicitly define the type.
For instance, consider the following:
let user: Record<string, string> = {
name: 'Peter Parker',
pet: 'Spider-Man'
}
type Y = {
key: string,
value: string
}
const values = Object.keys(user).map(([k, v]): Y => ({'key': k, 'value': v}));
// values: Array<Y>
In this example, the map function does not require explicit type declaration. However, the following also functions correctly:
const values = Object.keys(user).map(([k, v]) => ({'key': k, 'value': v}));
To summarize, simply specify the return type of your mapper function and it will be inferred accordingly.