Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index

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 
   }
  }
like image 209
Vanish Avatar asked Oct 18 '25 17:10

Vanish


1 Answers

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?

like image 165
Vanish Avatar answered Oct 20 '25 08:10

Vanish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!