Search code examples
typescriptnestjstypescript-class

How to declare a constant datatype using a class property datatype in typescript?


I'm creating a nestjs API so I'm using classes to declare my entities for example

export class Customer {
id: number;
name: string;
}

So now I'm working in my Customer Controller and I would like to type an get query param as customer.id because I'm thinking if some day the customer id data type changes to string automatically making my controller query param as string too.

@GET()
getCustomerById(@Params('id', id: Customer.id)) {
return this.customerService.getCustomerById(id))
}

Is it possible? Thanks


Solution

  • You can use TypeScript lookup types:

    getCustomerById(@Params('id') id: Customer['id']) {}