Skip to content

Generic type of superclass cannot be inferred from function call on a subclass #20348

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

Closed
bryanforbes opened this issue Nov 29, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Milestone

Comments

@bryanforbes
Copy link
Contributor

TypeScript Version: 2.7.0-dev.20171129

Code

class Evented<M> {
    // protected __typeMap__?: M; 
    on<K extends keyof M>(type: K, listener: (event: M[K]) => void) { }
}

function on<M, K extends keyof M>(target: Evented<M>, type: K, listener: (event: M[K]) => void) { }

interface EventMap {
    change: { type: 'change'; };
}

const e = new Evented<EventMap>();
e.on('change', (event) => { }); // type of event: { type: 'change' }
on(e, 'change', (event) => { }); // type of event: { type: 'change' }

class SubEvented extends Evented<EventMap> { }

const s = new SubEvented();
s.on('change', (event) => { }); // type of event: { type: 'change' }
on(s, 'change', (event) => { }); // type of event: M[K]

Expected behavior:
event is inferred as { type: 'change' } on line 20 (last line)

Actual behavior:
Line 20 is flagged as an error:

error TS2345: Argument of type '"change"' is not assignable to parameter of type 'never'.

Currently, the only way to get line 20 to work as expected is to uncomment line 2.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 29, 2017

Should be fixed by #20126

@mhegazy mhegazy added the Bug A bug in TypeScript label Nov 29, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 29, 2017

it is worth noting that the inference here for M will not be EventMap, since the there is no manifestation for this type to be inferred from. however, with #20126, M will be inferred from the use M[K] to be a type with a property named K (or "change" in the example above).

@mhegazy mhegazy added this to the TypeScript 3.1 milestone Jul 20, 2018
@RyanCavanaugh RyanCavanaugh modified the milestones: TypeScript 3.1, Future Aug 23, 2018
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Dec 16, 2021
@RyanCavanaugh
Copy link
Member

This is the intended behavior -- there is no inference site for M

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants