Victor Cui
Victor Cui

Reputation: 1705

Export interface as Partial<OtherInterface>

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>;

Upvotes: 4

Views: 1869

Answers (2)

coderHook
coderHook

Reputation: 57

You are trying to assign Partial to an interface which is not possible. What you can do is assign to other entity to be Partial of your interface.

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

const me: Partial<Iface> = {
  name: 'me-name'
 }

Basically, you can assign a type to another type and do what you wanted to do.

Upvotes: 0

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

Upvotes: 4

Related Questions