Skip to content

void parameter type produced from generic inference doesn't allow skipping as argument #39600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
maxim-grishaev opened this issue Jul 14, 2020 · 4 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@maxim-grishaev
Copy link

maxim-grishaev commented Jul 14, 2020

TypeScript Version: 3.7.2, 3.9.6, 4.0.0-beta

Search Terms: overload, void, indexed void, implicit void

Code

class Hooks<TMap extends Record<string, any>> { 
    one(a: keyof TMap, b: void) { }
    two<K extends keyof TMap>(a: K, b: TMap[K]) { }
}
const h = new Hooks<{foo: void}>()
h.one('foo'); // OK
h.two('foo'); // Expected 2 arguments, but got 1.(2554)

Expected behavior: both methods can be called with only one argument

Actual behavior: TMap[K] that is resolved to void does not allow skip argument

Although, this one works as expected:

// v1 works
interface EvtMap {
    one: string;
    two: void;
}

const createMethod = <N extends keyof EvtMap>(eventName: N) =>
    (ctx: EvtMap[N]) => null;

createMethod('one')('');
createMethod('two')();

// v2 fails
interface EvtMap {
    one: string;
    two: void;
}

const createMethod = <EvtMap>() =>
    <N extends keyof EvtMap>(eventName: N, ctx: EvtMap[N]) => null;

createMethod<EvtMap>()('one', '');
createMethod<EvtMap>()('two');

// generic method fails
interface TMap { 
    foo: void
}
class Hooks { 
    one(a: keyof TMap, b: void) { }
    two<K extends keyof TMap>(a: K, b: TMap[K]) { }
}
const h = new Hooks()
h.one('foo'); // OK
h.two('foo'); // Expected 2 arguments, but got 1.(2554)

Playground Link:

Related Issues: Not found

My StackOverflow question

Rationale for the pattern: semi-dynamic event management engine. Example

@maxim-grishaev maxim-grishaev changed the title Non-explicit void type does not allow to skip argument Implicit void type does not allow to skip argument Jul 14, 2020
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 14, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jul 14, 2020
@RyanCavanaugh
Copy link
Member

I'm not sure if this is fixable without some significant rework of the void parameter-skipping logic.

@RyanCavanaugh RyanCavanaugh changed the title Implicit void type does not allow to skip argument void parameter type produced from generic inference doesn't allow skipping as argument Jul 14, 2020
@jcalz
Copy link
Contributor

jcalz commented Aug 11, 2020

Is this related to #29131?

@maxim-grishaev
Copy link
Author

maxim-grishaev commented Aug 14, 2020

@jcalz Yes, to me it looks like the same issue

@kalkronline
Copy link

Here's a super concentrated example of this issue:

// This is an error, but it shouldn't be...
comp_ck<void>();
function comp_ck<T>(t: T) {}

// ...because this isn't.
void_ck();
function void_ck(t: void) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants