N
N
Ne7Le4Der2022-03-16 21:42:18
typescript
Ne7Le4Der, 2022-03-16 21:42:18

Is it possible to overload properties?

Is it possible to do something like this?

interface Field {
    a: string;
}
interface Field {
    a: string;
    b: string;
    c: string;
}


To allow TypeScript to create interface instances with properties a or a,b,c? Or just create a new interface via extends?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Crone1331, 2022-03-17
@Crone1331

Why not just make them optional?

interface Field {
  a: string;
  b?: string;
  e?: string;
}

B
black1277, 2022-03-16
@black1277

What was written (extension) is possible, but cannot be redefined:

interface Field {
    a: string;
}
interface Field {
    a: number;
    b: string;
    c: string;
}

That's not how it works, and even like this a: string | number won't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question