DEV Community

Discussion on: Keep Your Types And Interfaces Closer (In TypeScript)

Collapse
 
akshatbhargava123 profile image
Akshat

Hi, thanks for sharing.
I've a doubt related to typescript.

How do I manage a single variable with having simultaneously two types?
For example:
product: Product | string;
where string version would denote productId. It would be very useful when fetching populated fields via mongoose.

Collapse
 
antjanus profile image
Antonin J. (they/them)

good question! I think those two types are too vastly different to be in a union type and be useful.

I'd probably split it out to be productId: string and product: Product and just convert one to the other if necessary.

Collapse
 
akshatbhargava123 profile image
Akshat

Okay I got it, thank you sir.