F
F
fsmrdv2021-08-18 13:47:39
typescript
fsmrdv, 2021-08-18 13:47:39

How to rewrite generic TypeScript?

Hello!

There is such a generic.

export type SomeDeclaration<ParamsData, ResponseData> = {
    (params: ParamsData): SomeOtherType;
};

ResponseDatawas not used anywhere inside and the typescript will ignore it during compilation.

Here we can no longer extract it
type ExtractSomeDeclarationResponseDataType<T> = T extends SomeDeclaration<infer P, infer D>
    ? D
    : any;


If you ResponseDataduplicate it inside, then everything will work (example below), but I want to find a cleaner solution.

export type SomeDeclaration<ParamsData, ResponseData> = {
    (params: ParamsData): SomeOtherType;
    _r: ResponseData
};


What solution can you suggest?

UPD The problem is observed in version 4 of the typescript

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexandroppolus, 2021-08-18
@Alexandroppolus

"typescript": "3.8.3"
everything is retrieved even if no ResponseData is used. But why specify this unused parameter, I still do not understand

D
Dmitry Belyaev, 2021-08-19
@bingo347

Everything works, you are doing something wrong
https://www.typescriptlang.org/play?#code/C4TwDgpg...
In general, the unused generic argument for the sake of further infer is a fierce game, it can certainly be done differently, I can even help with this if you tell the details

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question