arvitaly
arvitaly

Reputation: 161

Why "void" not works for optional parameter, when i use generic type?

When i use a "void" type directly as function's parameter, it works good, but when i use generic, it not inferred.

I use TypeScript 3.4.4.

// It works perfectly :-)
function test(param: void){

}
test();
// It doesn't works :-(
type T = {
    a: {
        value: string;
    };
    b: {
        value: void;
    };
};
function test<KEY extends keyof T>(key: KEY, param: T[KEY]["value"]) {}
test("b"); // ERROR: Expected 2 arguments, but got 1. An argument for 'param' was not provided.

Upvotes: 1

Views: 181

Answers (1)

Mateusz Kocz
Mateusz Kocz

Reputation: 4602

This is a design limitation in the way the checks are implemented.

Checking for void parameters that can be elided is done prior to generic instantiation, which means that instantiations that produce void parameters are ignored, as is the case in fails.

This choice was made to avoid having to check every generic signature twice, and there were some issues with this breaking existing overload selections.

Source: https://github.com/Microsoft/TypeScript/issues/29131#issuecomment-449634318

Upvotes: 2

Related Questions