javascript - Typescript Element implicitly has an 'any' type because expression of type 'number'

My work environment isNuxt.js Vue works with some typescript.This problem occurs when for looping ins

My work environment is Nuxt.js Vue works with some typescript.

This problem occurs when for looping inside methods.

I refer to the _id in Objects reservatsions[i].I got the result from the _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's errors in the current syntax are as follows:

(property) reservations: ObjectConstructor

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'.Vetur(7053)

I don't know what type of error this is I tried to understand it while looking at the typescript, but I couldn't solve it.

The syntax for the error is as follows

const status = this.myObject[i].status; // string

const tripDate = this.myObject[i].date; // string

const id = this.myObject[i]._id; // number

In dev build, ERROR is generated, but the result was calculated and the desired result was obtained At pm2 start,

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 |             }

I seek advice on the reasons and solutions for this phenomenon

add myOject define below

data () {
 return{
    myObject: Object 
   }
  }

My work environment is Nuxt.js Vue works with some typescript.

This problem occurs when for looping inside methods.

I refer to the _id in Objects reservatsions[i].I got the result from the _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's errors in the current syntax are as follows:

(property) reservations: ObjectConstructor

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'.Vetur(7053)

I don't know what type of error this is I tried to understand it while looking at the typescript, but I couldn't solve it.

The syntax for the error is as follows

const status = this.myObject[i].status; // string

const tripDate = this.myObject[i].date; // string

const id = this.myObject[i]._id; // number

In dev build, ERROR is generated, but the result was calculated and the desired result was obtained At pm2 start,

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 |             }

I seek advice on the reasons and solutions for this phenomenon

add myOject define below

data () {
 return{
    myObject: Object 
   }
  }
Share Improve this question edited Jun 28, 2022 at 14:20 Vanish asked Jun 28, 2022 at 13:58 VanishVanish 1012 silver badges8 bronze badges 2
  • You can put your type inline (this.myobject as YourType)[i]._id; – Ilijanovic Commented Jun 28, 2022 at 14:06
  • It causes other errors. Conversion of type 'ObjectConstructor' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.Vetur(2352) – Vanish Commented Jun 28, 2022 at 14:12
Add a ment  | 

1 Answer 1

Reset to default 4

thx to reply bill.gates

(this.myobject as YourType)[i]._id;

This form works perfectly Instead, I put any in the type. It functions as I want

(this.myobject as any)[i]._id;

Is there a problem if I put any?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745585112a4634488.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信