Home > Back-end >  Is there a way to pull TypeScript types from an interface to use in a separate interface?
Is there a way to pull TypeScript types from an interface to use in a separate interface?

Time:08-26

I have one TypeScript interface like this:

interface SampleShape {
    prop1?: string;
    prop2?: string;
}

And I have a separate interface I want to use:

interface Payload {
    model: {
        name?: string;
        prop1?: string; // same as one in SampleShape
        prop2?: string; // same as one in SampleShape
    }
}

My use case is that I am given an object of SampleShape that I am transforming into a new Payload object. Is there a way to programmatically pull the types from SomeProperties so that I don't have to repeat myself?

CodePudding user response:

try this,

interface Payload {
    model: SampleShape & {
        name?: string
    }
}
  • Related