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

62 Views Asked by At

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

1

There are 1 best solutions below

0
Micael Levi On BEST ANSWER

You can use TypeScript lookup types:

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