【发布时间】:2023-03-06 15:06:01
【问题描述】:
我正在学习打字稿,我已经编写了非常基本的代码。
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();
我曾经在哪里使用过该数组,但出现以下错误:
错误 TS2314:通用类型“数组”需要 1 个类型参数 我哪里做错了?
【问题讨论】:
-
这不是您提供特定类型的方式 - 您的意思是
Array<string>吗?或者只使用string[]。 -
尝试像这样定义数组:
nameOfStudents : string[] = [];
标签: typescript