Hi, I'm Jacob. Enjoying devFlipCards? Buy me a coffee

16. How does the Record utility type work? Provide an example.

The Record type in TypeScript is a built-in utility type that allows you to create objects with specific keys and value types. Record<K, T> is equivalent to an object where the keys are of type K and the values are of type T.

Example:

type Person = Record<'name' | 'age', string>; const person: Person = { name: 'John', age: '25' };

In this example, we create a Person type that has the keys name and age, and the values must be of type string. The Record type is useful when we want all keys in the object to have the same value type.

You can also use Record with more complex types:

type ErrorCodes = Record<'404' | '500', { message: string; status: number }>; const error: ErrorCodes = { '404': { message: 'Not Found', status: 404 }, '500': { message: 'Internal Server Error', status: 500 } };

In this case, the keys 404 and 500 are associated with objects containing error details.

Struggling to find common date to meet with your friends? Try our new tool
commondate.xyz