Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inferring a second type with a default

Can I define a function generic over two types, one of which has a default, in such a way that the default gets inferred from the usage of the function if I specify the first type?

Example:

const f = <T, N extends number = number>(num: N, val: T): [N, T] => [num, val];

const e1 = f(1, "a");         // e1: [1, string]      <- as expected
const e2 = f<string>(1, "b"); // e2: [number, string] <- actual
                              // e2: [1, string]      <- desired

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Not possible currently.

You cannot partially infer generic parameters as of Typescript 4.6.2. That means that are either all inferred from usage, or all explicit.

See this github issue: https://github.com/microsoft/TypeScript/issues/10571

This cased is totally inferred:

const e1 = f(1, "a");

And this case is totally explicit:

const e2 = f<string>(1, "b");

Even though you only provide one parameter, this puts the generics in the explicit mode and it searches for other explicit sources of that parameter. And it finds one in the default type of N, so it uses that.


The typical work around here is nested functions, which may be uglier than you want:

const fComposed = <N extends number = number>(num: N) => {
    return <T>(val: T): [N, T] => {
        return [num, val]
    }
}

const e3 = fComposed(123)('test') // [123, string]
const e4 = fComposed(123)<string>('test') // [123, string]

Playground


All that said, this example is pretty contrived so I don’t know your actual use case. But there may be a better approach entirely depending on your goal.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading