Home > OS >  How do I define object data type in parameter?
How do I define object data type in parameter?

Time:08-10

My code like this :

public async index(params: any) {
    return params
}

return params like this :

{
    "data": {
        "params": {
            "task_id": 101
        }
    }
}

I want to define object data type in parameter. So it does not use any

I try like this :

public async index(params: object) {
    return params
}

But there exist message : Property 'params' does not exist on type 'object'

How can I solve this problem?

CodePudding user response:

You can solve this problem by defining an own type that fits your object. In your example, this would be:

type paramsType = {
   data: {
      params: {
         task_id: number
      }
   }
}

Then you can write (params: paramsType). Let me know if this works. :)

CodePudding user response:

Try like this:

type myParamsType = {
   data: {
      params: {
         task_id: number
      }
   }
}

public async index(params: myParamsType) {
    return params;
}

// Use like this.

myParams: myParamsType = {
    "data": {
        "params": {
            "task_id": 101
        }
    }
};

index(myParams);

You can also define a class instead of a type. Depedns of what you have to do with datas.

  • Related