Sorry about the question title, I am not quite sure how to phrase it.
I am fairly new to Typescript and wanted to add strong types to a new project.
Lets say I have a function that accepts two parameters: value and params written like
type Value = 'category' | 'follows' | 'users';
request(value: Value, params: object)
As you can see Value type only accepts three predefined values. But params should be conditionally typed when value changes:
If value is category, then params should type as { author: string, year: number }, if it is users then it should type { id: number } OR { name: string }
And that's the whole challenge of it. I can do it with objects and indexes but have been stuck with types.
Is it even possible to do this ?
Thanks a lot !
NVH
EDIT: Thanks a lot for all your help, I had a good time reading all of the documentation as well, since I want to have the safest types for my function I will go for @captain-yossarian solution but the others worked as well!