Closed
Description
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.