Typescript:error TS2314: Generic type 'Array<T>' requires 1 type argument(s)
11,719
Solution 1
Generic arrays must be defined as:
const arr = new Array<string>()
const arr = string[]
Solution 2
Using the T[] sythax is recommended over Array< T > synthax
TSlint rule "array-type": [true, "array"]:
For your code, Array[string] should be Array < string > in order to compile.
nameOfStudents: Array<string>;
noOfteachers: number
constructor(name: Array<string>, no: number) {
this.nameOfStudents = name;
this.noOfteachers = no;
}
The best practice would be something like this
class School {
nameOfStudents: string[];
noOfteachers: number;
constructor(name: string[], no: number) {
this.nameOfStudents = name;
this.noOfteachers = no;
}
printName(): void {
for (const studentName of this.nameOfStudents) {
console.log(studentName);
}
}
I higly recommend you to install tslint for your project, it will help you to write clean Typescript code.

Author by
AConsumer
Updated on June 28, 2022Comments
-
AConsumer 11 months
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 ?