Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i cant define a value for students because of typescript error Type '{ Id: number; Name: string; }' cannot be used as an index type

i cant define a value for students because of typescript says it cant be used as index type note : i declared id as number not Number and Name as string not String

import { Component } from '@angular/core';
interface student {
  Id: number;
  Name: string;
}
@Component({
  selector: 'sameh-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  students: Array<student> = [];
  color: string = 'color: red';
  color2: string = 'color : blue';
  userName: string = '';
  title = 'ng-arab';
  constructor() {
    this.students[{ Id: 0, Name: 'sameh' }];
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I guess you are trying to push an element inside the array on the constructor, change your code to.

import { Component } from '@angular/core';
interface student {
  Id: number;
  Name: string;
}
@Component({
  selector: 'sameh-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  students: Array<student> = [];
  color: string = 'color: red';
  color2: string = 'color : blue';
  userName: string = '';
  title = 'ng-arab';
  constructor() {
    this.students.push({ Id: 0, Name: 'sameh' }); // <- changed here
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading