2

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 ?

1 Answer 1

1

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[];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.