7

I want to merge two types and also keep the generics valid. for example

Type 1

interface Request<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query> extends core.Request<P, ResBody, ReqBody, ReqQuery> { }

Type 2

type Auth = {
  user: User
}

I want my new type to be a merge of these two, where the type can receive all those generics.

Right now I am just merging them, but then I am not able to use generics of the Request type.

Currently I am just merging these two.

export type Merge<FirstType, SecondType> = Omit<FirstType, keyof SecondType> & SecondType;

Of course I can do all this manually, but this case is repeating in my project, I want a utility method which can do this for me. For example

MergeWithGenerics<Request, Body>

Where the new type keeps all generics of the first arg.

I don't know if its possible or not, but it would be really great if someone can help me with this.

3
  • is the goal here to not have to repeat the default types for your generics? Commented Apr 27, 2020 at 18:12
  • 3
    I'm sorry, pretty sure this is not possibile in TS right now. The best thing you can do ` type MergedGen<P, TRes, TReq, TQuery> = Merge<MyRequest<P, TRes, TReq, TQuery>, Auth>;`, but this is clearly manual Commented Apr 27, 2020 at 18:13
  • 1
    please make your question more concrete, preferably with a typescript playground link Commented May 2, 2020 at 19:35

2 Answers 2

1

You can't pass a generic type (without parameters) to another generic type.

interface Generic<P> { }
type Id<T> = T
type G = Id<Generic>; // Generic type 'Generic<P>' requires 1 type argument(s).(2314)

That being said, your definition of Merge seems to be a pretty good shorthand for what you're trying to do. Merge<Request<Params, ResBody, ReqBody, Query> & Auth> doesn't seems a lot bigger then RequestWithAuth<Params, ResBody, ReqBody, Query>.

A variation of the proposal from @leonardfactory, might also simplify a bit:

 type ExtendedRequest<Extension, P, TRes, TReq, TQuery> = Merge<Request<P, TRes, TReq, TQuery>, Extension>
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you could use a snippet - assuming you're using VSCode or similar.

With following VSCode snippet, you can type "mgen" and tab complete (ctrl+space), then tab on the snippet name. Each time you push tab, cursor will iterate to next variable positions.

snippets.json

{

"Merged Generic": {
    "prefix": "mgen",
    "body": [
        "export type Merge<$1, $2> = Omit<$1, keyof $2> & $2;",
    ],
    "description": "shorthand for defining merged generics"
}

} enter image description here

In your case you probably want 3 or 4 variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.