Currently in the process of converting my javascript
project to typescript
, and I must say, the learning stage has been quite enlightening. Converting the project has taught me a lot so far. While I did encounter a challenge in my code which I managed to resolve, the issue now is that I have multiple conditions for it, and extending 4 or 5 times for the same result seems impractical.
Below is the code snippet:
type GetModel<M> = IsUnion<M> extends true
? UserModel
: M extends typeof types.FRONT
? UserModel
: AdminModel;
Playground: Playground Link
I am aiming to simplify the above code into something more linear, like the example below, but for some reason, it's not functioning as expected:
type GetModel<M> = IsUnion<M> extends true | M extends typeof types.FRONT
? UserModel
: AdminModel;
My goal is to achieve the same result as demonstrated in the playground link above.
type user = GetModel<1 | 2>; // user
type user1 = GetModel<2>; // user
type admin = GetModel<1>; // admin