interface TypeA {
a: string
b: string
}
interface TypeB {
c: string
}
Jetzt möchte ich einen TypeC machen, a und b sind optional, c ist erforderlich.
interface TypeC {
a?: string
b?: string
c: string
}
Gibt es eine Abkürzung, um dies zu machen? zum Beispiel:
// type TypeC = TypeA? & TypeB
Ich denke, es ist nützlich, wenn TypeA riesig ist
Lösung des Problems
Spielplatz
interface TypeA {
a: string
b: string
}
interface TypeB {
c: string
}
// First way using extends and Partial
interface TypeC extends Partial<TypeA>, TypeB {
//...
}
// Second way using & and Partial
type TypeD = Partial<TypeA> & TypeB;
const variableC: TypeC = {
c: '',
};
const variableD: TypeD = {
c: '',
};
Keine Kommentare:
Kommentar veröffentlichen