Tip: TypeScript Utility Types Suppose you have a javascript object but the object is dynamic, except few properties, you don't know what other properties are exactly
For example
let person = { id: 1, someAttribute: 'somevalue' } // In this object, only id is certain attribute.
Other attributes like name, age, city, address along with some unknown attributes are dynamic and their types are also unknown
In this case to declare a type, we can use Index Signatures
interface Person {
id: number;
[key: string]: any;
}
Now you can assign any object to person variable with id as mandatory attribute
But, there is another better way which is Record<Keys,Type> utility type
So, we can write above interface as
interface Person extends Record<string, any> {
id: number
}
The end result is same, we can assign any object to person variable with id as mandatory attribute
Example: https://bit.ly/3C9ecD9 (edited)