Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export interface as Partial<OtherInterface>

Tags:

typescript

I have an interface defined like below:

export interface Iface {
   id: string;
   foo: string;
}

I want to create an interface that's a partial of Iface and export it. I tried

export interface PartialIface = Partial<Iface>;

but typescript complains of a partial error. If I change the interface keyword to type it works. How come I have to declare it as a type alias and not an interface?

export type PartialIface = Partial<Iface>;
like image 381
Victor Cui Avatar asked Oct 30 '25 05:10

Victor Cui


1 Answers

Please take a look:

export interface Iface {
   id: string;
   foo: string;
}

export type PartialFace=Partial<Iface>

Also You can copy paste your interface and make all properties optional:

export interface Iface {
   id?: string;
   foo?: string;
}

I'm not aware about other approaches

like image 101
captain-yossarian Avatar answered Oct 31 '25 21:10

captain-yossarian