0

I have an Angular Material Table. Obviously, I have an interface from which is created the dataSource for the table. Is there any possibility to add a new value dynamically in the interface, or at least to create the interface dynamically? Because the data which is coming from the backend it's unknown.

Example: from this:

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

to this:

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
  index: number;
}

or from this:

export interface PeriodicElement {}

to this:

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
  index: number;
}
1
  • hhhmmm If it's almost same interface but with different number of fields present, you might use DataSource<Partial<PeriodicElement>>(elements) Commented Sep 15, 2021 at 5:00

1 Answer 1

2
  1. It will be better to use it like,
export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
  [key: string]: string | number;
}

or just

export interface PeriodicElement {
  [key: string]: string | number;
}
  1. Otherwise you can create separate models and use union(|) operator.

  2. Also you can use Partials to achieve this.

  3. For creating dynamically Refer here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.