How to declare an extended type in Typescript

Asked

Viewed 82 times

1

Hello, I have the following situation:

// essa classe contém apenas 2 propriedades
interface Foo {
    a: string;
    b: number;
}

// em determinada situação, eu preciso declarar
// a interface junto com mais uma propriedade, assim
let Bar: ({ c: boolean } implements Foo); // não funciona
let Bar: { c: boolean } implements Foo;   // não funciona
let Bar: { c: boolean } implements Foo;   // não funciona
let Bar: { c: boolean } && Foo;           // não funciona
let Bar: [{ c: boolean } implements Foo]; // não funciona
let Bar: [{ c: boolean } && Foo];         // não funciona

the only way I understand that it is possible for me to do this would be to declare the interface again Bar thus

interface Foo {
    a: string;
    b: number;
}
interface Bar extends Foo {
    c: boolean;
}
let Bar:Bar;

Is there any way to extend the properties of a type in the statement (without the need to create an external interface? I do not believe it is fully functional I make the declaration of a new interface that does this only to meet this situation.

  • 1

    It doesn’t work because this syntax doesn’t make any sense and doesn’t do what you want, it’s an invention, it’s not that it doesn’t allow you to extend the type.

  • 1

    @Maniero, yes his statement does not exist, but has a correct syntax for it, as I presented in the reply, although I have never used, I prefer to declare an extended interface.

  • 1

    @Fernandoleal yours is correct, I just don’t know if it solves what he wants.

1 answer

1


You could do something like this (which will unite the definitions of the two statements, which seems to be your intention):

let Bar: { c: boolean } & Foo;

Then you can access the properties normally:

let bar: { c: boolean } & Foo = {} as any;
bar.c = true;
bar.a = "hello";
bar.b = 2;

Example in Stackblitz.


Another interesting and practical way is to declare a new type:

type Bar = { c: boolean } & Foo;

And I could use it as follows, for example:

let bar2: Bar = {} as any;
bar2.c = false;
bar2.a = "world";
bar2.b = 1;

With the type definition, it becomes easier to reference this same type in other parts of the code, and even facilitate refactoring and maintenance.


I don’t know the correct term for this type of statement (if any know or have any references, please comment or edit), but I followed the same idea of multiple types for a variable (Example: let x: string | number), where that variable approach shall contain a value of the type string or number.

I do not find this approach very useful, because it seems more interesting to create a new interface extending from Foo, as you presented in the question. So you will already have this new type set to be declared everywhere where it is needed. Even so the simplified declaration can be used for specific situations.

  • That’s exactly what I needed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.