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.

devFlipCards 2025

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.