![](/img/trans.png)
[英]TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
[英]Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index
我的工作環境是 Nuxt.js Vue 使用一些打字稿。
當循環內部方法時會出現此問題。
我參考了 Objects reservations[i] 中的 _id。我從 _id 得到了結果
methods: {
async tripdone() {
try {
const legnth = this.myObject.length;
for (let i = 0; i < legnth; i++) {
const status = this.myObject[i].status;
const tripDate = this.myObject[i].date;
const today = new Date();
var year = today.getFullYear();
var month = ("0" + (today.getMonth() + 1)).slice(-2);
var day = ("0" + today.getDate()).slice(-2);
var todayString = year + "-" + month + "-" + day;
if (tripDate < todayString && status === "confirmed") {
const id = this.myObject[i]._id;
const payload = {
status: "tripOver",
};
this.$axios.put(`/api/myObject/${id}`, payload);
const message = id;
const type = "success";
this.$message({ message, type });
}
}
} catch (e) {
const message = "error";
const type = "error";
this.$message({ message, type });
}
}
Vetur 在當前語法中的錯誤如下:
(property) reservations: ObjectConstructor
元素隱含地具有“任何”類型,因為“數字”類型的表達式不能用於索引類型“對象構造器”。 在“ObjectConstructor”類型上找不到帶有“number”類型參數的索引簽名。Vetur(7053)
我不知道這是什么類型的錯誤,我在查看typescript
時試圖理解它,但我無法解決它。
錯誤的語法如下
常量狀態 = this.myObject[i].status; // 細繩
常量 tripDate = this.myObject[i].date; // 細繩
常量 id = this.myObject[i]._id; // 數字
在dev build中,產生了ERROR,但是計算了結果,得到了想要的結果在pm2啟動時,
ERROR in index/myObject/index.vue:360:26
TS7053: Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'ObjectConstructor'.
No index signature with a parameter of type 'number' was found on type 'ObjectConstructor'.
358 | const legnth = this.myObject.length
359 | for (let i = 0; i < legnth; i++) {
> 360 | const status = this.myObject[i].status
| ^^^^^^^^^^^^^^^^^^^^
361 | // const legnth = this.myObject.length
362 | // for (let t = 0; t < legnth; t++) {
363 | const tripDate = this.myObject[i].date
ERROR in index/myObject/index.vue:363:28
TS7053: Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'ObjectConstructor'.
No index signature with a parameter of type 'number' was found on type 'ObjectConstructor'.
361 | // const legnth = this.myObject.length
362 | // for (let t = 0; t < legnth; t++) {
> 363 | const tripDate = this.myObject[i].date
| ^^^^^^^^^^^^^^^^^^^^
364 | const today = new Date()
365 |
366 | var year = today.getFullYear();
ERROR in index/myObject/index.vue:377:24
TS7053: Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'ObjectConstructor'.
No index signature with a parameter of type 'number' was found on type 'ObjectConstructor'.
375 | // return tripDate < todayString
376 | if ( tripDate < todayString && status ==='confirmed') {
> 377 | const id = this.myObject[i]._id
| ^^^^^^^^^^^^^^^^^^^^
378 | const payload = {
379 | status: "tripOver"
380 | }
求教出現此現象的原因及解決辦法
在下面添加 myOject 定義
data () {
return{
myObject: Object
}
}
謝謝回復 bill.gates
(this.myobject as YourType)[i]._id;
這種形式完美地工作相反,我把any
類型。 它按我的意願運作
(this.myobject as any)[i]._id;
如果我放any
有問題嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.