I want to write an interface or type with typescript to enforce the type of known fields, and also some dynamic ones.
Example:
const person = {
children: [person1, person2],
name: "X",
dynamicField1: person3,
dynamicField2: person4,
dynamicField3: person5
}
// Something that would look like this
interface Person {
name: string;
children: Person[];
...rest: Person;
}
Is it possible to to that in typescript?
Is it possible to to that in typescript?
Yep, you can have your Person
interface extend a record which allows any key/value like this:
interface Person extends Record<any, any> {
name: string;
children: Person[];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.