Repository: TypeScript
Issue: an emitter with 239 events breaks type check, throw ts2590 Expression produces a union type that is too complex to represent
Labels: Needs Investigation,Domain: Contextual Types,Domain: Big Unions

# Bug Report

I wrote a framework (namely [koishi](https://github.com/koishijs/koishi)) which uses a fully-typed event emitter implemented by itself. However when I create more events (totally 239), the type check just broke with an error ts2590 (Expression produces a union type that is too complex to represent). It seems that the error was thrown here:

![image](https://user-images.githubusercontent.com/33423008/107846539-7885ac80-6e1f-11eb-8fba-28b2ead4e848.png)

I know 239 events is a lot, but I still cannot figure out how the 239 events became "100000 type checks".

Can you help prevent this error either by optimize type check performance or by improve my code? Thanks.

### 🔎 Search Terms

union type, interface, event emitter, ts2590

### 🕗 Version & Regression Information

I tried with v4.1.3, v4.1.5 & v4.2.1-rc. They all failed.

### ⏯ Playground Link

This playground will not work because it imports a dependency `@octokit/webhooks-definitions` (it's just a simple collection of github webhooks type definitions, but it is too big (over 5000 lines) to be included as I thought).

[Playground link with a dependency](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgUQG4FMB2MAKBDATwBsI8ATAWTzABo4B1dAIwAsIIBrNLGAOTxDo4AXzgAzKBBBwA5AAEIAYxidgMAPQB3Zm04BnALRl0Y4JjXAImPer2KW6EHhkAoF8cVE8UITAJghAFVzKwAVCABJbHQoPXRlS0wAHkCAPjgAXjgACkC4dAAPGCwyPTg8TAI4AH4cjnQCAC44QIBKTPTUCGAyOGbMdAwodsLizFK6huazMRi4CPaMzu7e2oi+uAGhgG53eK8fOD8AuAAxLxgxpNC6AGl8opKy+oIIMThQzLgXt4-0rOCiXCUWKsXiMES1wA2rcALqpXYeA6+fxCKIDKC3Bp6a53B5jCY-d6fLJEv5fMnnPCXLDXdIAMm+DV+VJpyRucFuCLchUgsDgSO8QkwAnQejAeEUQgAymK9IlEC44EdUXAAOJYHxlLIyCTAEoyOAAH1k9gqAyIhpNMgA5pIAK5gK2yO0QR0GQQgJgxZ22h1gAySIjoX2u92mYOh-0GRwQABWwBku2VxyE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ChAlTgKeG0agWB7Uxggdep4+bZAlMfMgzQ-7byjr7MgB83CwVYhueBPA8FnwmPTbmjM6BkKkdQESn19hCnEPaTAD04AJXuOI54rURG4OoKkbIIpBBIzoEQYAMMsAxGaAzABcJWh2yjkwdgwYKhuCsNkfsAEmwyDoNkKO75WguCAA)

And the dependency code can be found here: https://unpkg.com/@octokit/webhooks-definitions@3.60.0/schema.d.ts

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
interface EventMap extends SessionEventMap, WebhookEventMap {}

declare function on<K extends keyof EventMap>(name: K, listener: EventMap[K]): () => boolean

on('message', () => {})
```

### 🙁 Actual behavior

TS2590 error: Expression produces a union type that is too complex to represent.

### 🙂 Expected behavior

No compiler error