Skip to content
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

TS2345 not thrown when calling a function whose parameter is a lookup type of a generic type #38563

Closed
jiakaiz-g opened this issue May 13, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jiakaiz-g
Copy link

jiakaiz-g commented May 13, 2020

TypeScript Version: 3.9.2

Search Terms:

TS2345
generic type
lookup type

Code

interface AEvent {
  foo: number;
}

interface BEvent extends AEvent {
  bar: number;
}

export declare interface AEventMap {
  'event': AEvent;
}

export declare interface BEventMap extends AEventMap {
  'event': BEvent;
}

class A<EventMap extends AEventMap = AEventMap> {
  listeners: Array<(e: EventMap['event']) => void> = [];

  addListener(listener: (e: EventMap['event']) => void) {
    this.listeners.push(listener);
  }

  triggerEvent(e: EventMap['event']) {
    for (const listener of this.listeners) {
      listener(e);
    }
  }

  run() {
    this.triggerEvent({ foo: 1 });  // Should throw TS2345.
  }
}

class B extends A<BEventMap> {}

const b = new B();
b.addListener((e) => console.log(e.bar));  // 'bar' may not exist.

b.run();

Expected behavior:

TS2345 should be thrown at the this.triggerEvent({ foo: 1 }); statement because EventMap['event'] could be a subtype of AEvent, in which case { foo: 1 } cannot assign to it.

A simplified version of the code without lookup types gets an error correctly.
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgIIFEBuFzIN4CwAUMsjAPbkBcyIArgLYBG0A3MQL7HGiSyIoAQlhxhkEAB6QQAEwDOaEbkIlkTOFBr1mbTtyIIANnDkLUAHiVjJ0+Yuy4AvPdEA+fMVKHgc6dDk0qFBQcACe5gAUEDRWAJTIju6Y5MAy7s4A2gC67ESeyHAyMgAyPn5QEd6+ONA0UTEOYPGJyMmp8SqkpGAAFj4AdFXlcv0ADnRyPZVlNVCxuaRceapgUMAA5uvQVvXIcR6qpBRQyBEI5CC+yEOzyOQwyL0DNyD+Hfld1zOvFRDzH8glot9KQoHQQBF3odHn0RqsNlsoDs8GRKDQAIyA+akAD0OOQABUelByAB3BQEgDKACYAMwAFgArP18kslsQjCYFIJxFIcHYLMJGu48OyDBcrkwErQIKTkIJIbkmP1CiVvtAIlFmu5zpdyIYIINyOsov11HNscg8cgAOTmm3IBhhWjkawSMos5bKsEQ-5EIA

Actual behavior:

No compilation error.

Playground Link:

https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgIIFEBuFzIN4CwAUMsjAPbkBcyIArgLYBG0A3MQL7HGiSyIoAQlhxhkEAB6QQAEwDOaEbkIlkTOFBr1mbTtyKSADuShiZEBABsNKXtHhJF2cAFk4h-MVIByCM7DeNBj+7ERcRMRGJmYW1lC24PYCyML+bh6S0vJOoumeqr7+gSlKYKHhxFZwcgqoADyleZk42cG57sgAvDmu7gB8+aSWwHLS0HJBUFBwAJ51ABQQNI3uANqFot4AugCUXQOY5MAyA92rW6FeyHAyMgAyI2NQ88OjONA0i8tpaxvg23tOgcjjI9ipSKQwAALEYAOleTzksMMdDkUJej3eUB2oVIFVUYCgwAA5sToKUvsgVoZ1n5NrtBhCKFBkPMEOQQKNkAischyDBkNC4TyQOMwVcIdzMaLnhAcRLkOE8fpSFA6CB5uLVJCYUjCSSyVAKXgyJQaABGRU40gAehtyAAylDyHRLDJBVCoOQAO7IAAqDoATABmAAsAFZYVdwviqjUUuIpC1anVUu1DAM8LGOVymF1aBBfYJNaEmLCbvdpdB5otAQN2ZzyJYIPDyMTFrD1Njrcg7chvF3vMgGLNaOQxJJHlGIkQy2qNfKiEA
Related Issues:

I could not find.

@jack-williams
Copy link
Collaborator

Here is a good explanation of the behaviour: #27470 (comment)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 14, 2020
@jiakaiz-g
Copy link
Author

Thank you for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants