Reputation: 4053
Let's have following code:
interface A {
a: number;
}
// doesn't work - "An interface may only extend a class or another interface."
// interface AOpt extends Partial<A> {}
// does work, but leads to code duplication :(
interface AOpt {
a?: number;
}
interface B extends AOpt {
b: number;
}
How to create an interface in a way Partial
works with type
, but so it is extend-able by an interface?
Upvotes: 5
Views: 1171
Reputation: 23702
It seems our needs will be covered by the next release of TypeScript, with the pull request "Allow deriving from object and intersection types":
type T1 = { a: number };
type T2 = T1 & { b: string };
// ...
type Named<T> = T & { name: string };
interface N1 extends Named<T1> { x: string } // { a: number, name: string, x: string }
interface N2 extends Named<T2> { x: string } // { a: number, b: string, name: string, x: string }
interface P1 extends Partial<T1> { x: string } // { a?: number | undefined, x: string }
In the roadmap, it appears under the label "Improved support for "mixin" patterns", which is checked. Therefore, it should be available in typescript@next
.
Upvotes: 3