Typescript:error TS2314: Generic type 'Array<T>' requires 1 type argument(s)

Issue

I am learning typescript and i have written very basic code.

class School {

    nameOfStudents: Array[string];
    noOfteachers: number

    constructor(name: Array[string], no: number) {
        this.nameOfStudents = name;
        this.noOfteachers = no;
    }

    printName():void{

        for(let i=0;i<this.nameOfStudents.length;i++){
            console.log(this.nameOfStudents[i])
        }
    }
}
let arr=["a","b","c","d","e"]
let school = new School(arr,100);

school.printName();

Where ever i have used the array i am getting the following error:

error TS2314: Generic type ‘Array’ requires 1 type argument(s)
Where i am doing wrong ?

Solution

Generic arrays must be defined as:

  • const arr = new Array<string>()
  • const arr = string[]

Answered By – Oscar Paz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published