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

Maximum call stack size exceeded #42677

Closed
didinele opened this issue Feb 6, 2021 · 1 comment
Closed

Maximum call stack size exceeded #42677

didinele opened this issue Feb 6, 2021 · 1 comment

Comments

@didinele
Copy link

didinele commented Feb 6, 2021

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

  • This is a crash
  • This is the behavior in every version I tried
  • I was unable to test this on prior versions because it depends on the brand new template literal types introduced in 4.1

⏯ Playground Link

Playground link with relevant code

💻 Code

import { EventEmitter } from 'events';

interface Test<T> {
    on(event: 'error', listener: (error: any) => any): this;
    on(event: `__${string}`, listener: (data: string, isError: true) => any | ((data: T, isError: false) => any)): this;

    once(event: 'error', listener: (error: any) => any): this;
    once(event: `__${string}`, listener: (data: string, isError: true) => any | ((data: T, isError: false) => any)): this;

    emit(event: 'error', error: any): boolean;
    emit(event: `__${string}`, data: string, isError: true): boolean;
    emit(event: `__${string}`, data: T, isError: false): boolean;
}

class Test<T> extends EventEmitter {
    public test() {
        // In my case - I was using a package that could provide null, or any type of data, so they used `any` - but I was expecting a string.
        const data: any = 'test';
        const key = `__${data as string}` as const;

        // In my case, I had 2 values that were nullable - the error, or the data. One of them is guranteed to be present, so "(a ?? b)!" is the way to go.
        const nullableProp1: T | null = 'test' as any;
        const nullableProp2: string | null = 'test' as any;

        // If you start writing out this line, it will compile with regular type errors until the 2nd parameter, at which point everything breaks.
        if (key) this.emit(key, (nullableProp1 ?? nullableProp2)!, true as boolean);
    }
}

🙁 Actual behavior

The Typescript compiler fails to pick an overload, and seems to recurse down past the maximum stack call size. This is reflected in the browser console when on the playground, and of course, locally

🙂 Expected behavior

The Typescript compiler successfully picks an overload and continues on.

@didinele didinele changed the title Maximum call stack exceeded. Maximum call stack size exceeded Feb 6, 2021
@didinele
Copy link
Author

didinele commented Feb 6, 2021

Apologies, just tried typescript@next and it seems resolved in 4.2.0-dev.20210206.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant