question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EventEmitter types still broken on Typescript 3.9 RC

See original GitHub issue

Typescript 3.9, which should be out some time this week, still has an error when compiling

Here’s my tsconfig:

{
    "compilerOptions": {
        "strict": true,
        "lib": ["esnext", "dom"],
        "types": []
    }
}

And my index.ts:

import eventemitter3 = require("eventemitter3");

The error is here:

  export type EventListener<
    T extends ValidEventTypes,
    K extends EventNames<T>
  > = T extends string | symbol
    ? (...args: any[]) => void
    : (...args: ArgumentMap<T>[K]) => void; // error

“Type ‘K’ cannot be used to index type ‘ArgumentMap<T>’.”

I can’t tell what’s going wrong from a quick glance at the types; there are too many layers of indirection here. My guess is that the compiler just can’t find a declaration that says K is a legal key of ArgumentMap<T> — but I couldn’t figure out what declaration would work.

Discovered in the Typescript nightly tests, where we compile our nightly against the latest shipped types of widely used packages: https://github.com/microsoft/TypeScript/pull/38405

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sandersncommented, May 11, 2020

That works for me, and is basically the workaround discussed in the PR that introduced the breaking change, which means that it should be a pretty safe change.

0reactions
gfmiocommented, May 11, 2020

I think I’ve found a solution by using Exclude.

  export type ValidEventTypes =
    | string
    | symbol
    | { [K in string | symbol]: any[] | ((...args: any[]) => void) };

  export type EventNames<T extends ValidEventTypes> = T extends string | symbol
    ? T
    : keyof T;

  export type ArgumentMap<T extends object> = {
    [K in keyof T]: T[K] extends (...args: any[]) => void
      ? Parameters<T[K]>
      : T[K] extends any[]
      ? T[K]
      : any[];
  };

  export type EventListener<
    T extends ValidEventTypes,
    K extends EventNames<T>
  > = T extends string | symbol
    ? (...args: any[]) => void
    : (...args: ArgumentMap<Exclude<T, string | symbol>>[K]) => void;

Can you verify that this works for you as well and that this won’t cause any unintended side effects?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - TypeScript 3.9
This goal of this type operator is to accurately model the way that Promise unwrapping works in JavaScript. We initially anticipated shipping awaited...
Read more >
"Type 'EventEmitter' is not generic" ERROR in angular
Even in the angular website it looks like that code is correct. I'm currently using Angular CLI: 1.7.4; Node: 8.11.1; Typescript: 2.8.1.
Read more >
web3-core-promievent | Yarn - Package Manager
This is the PromiEvent package used to return a EventEmitter mixed with a Promise to allow multiple final states as well as chaining....
Read more >
Become a ninja with Angular sample - Angularjs - 16
instead (2016-06-09) Pipes • Date pipe is now fixed in rc.2, no more problem with Intl API (2016-06-16) Styling components and encapsulation •...
Read more >
TypeScript Programming with Visual Studio Code
js , you'll see that it doesn't look very different from helloworld.ts . The type information has been removed and let is now...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found