Skip to content

Rozszerzanie typów

Można rozszerzyć interface (skopiować elementy składowe z innego typu):

interface X {
a: string;
}
interface Y extends X {
b: string;
}

Można również rozszerzać wiele typów:

interface A {
a: string;
}
interface B {
b: string;
}
interface Y extends A, B {
y: string;
}

Słowo kluczowe extends działa tylko z interfejsami i klasami; w przypadku typów należy użyć przecięcia:

type A = {
a: number;
};
type B = {
b: number;
};
type C = A & B;

Można rozszerzyć typ za pomocą interfejsu, ale nie odwrotnie:

type A = {
a: string;
};
interface B extends A {
b: string;
}