Skip to content

Naked generic type returned from iterator method #59161

Closed

Description

@reverofevil

🔎 Search Terms

generic generator static

🕗 Version & Regression Information

  • I was unable to test this on prior versions because there is still no online version of every-ts bisect

⏯ Playground Link

Link

💻 Code

class Base {
    static *[Symbol.iterator]<T>(this: T) {
        yield this;
    }
}
class Child extends Base {}
const test = function* () { yield* Child };
const value = [...test()][0]; // T
console.log(value); // Child

🙁 Actual behavior

The result is typed as T, whatever generic parameter outside of generic code means.

🙂 Expected behavior

T should be instantiated with Child during a call to [Symbol.iterator]

Additional information about the issue

For regular static method calls it works as expected, by instantiating T

class Base {
    static foo<T>(this: T) {
        return this;
    }
}
class Child extends Base {}
const value = Child.foo(); // Child
console.log(value); // Child

On the other hand, this is a nice way to create unique types for opaque type implementation! 🙃

Activity

  1. changed the title [-]Naked generic type in this type for generator static method[/-] on Jul 7, 2024
  2. Andarist commented on Jul 7, 2024

    @Andarist
    Contributor

    This is a fun one too:

    class Base {
      static *[Symbol.iterator]<T extends [1, 2]>() {
        yield {} as T;
      }
    }
    class Child extends Base {}
    const test = function* () {
      yield* Child;
    };
    const value = [...test()][0];
    //    ^? const value: T extends [1, 2]

    Basically, all type parameters leak in this situation.

  3. reverofevil commented on Jul 7, 2024

    @reverofevil
    Author

    Also it doesn't have to be static. I left it in the example, because otherwise it's harder to see that the behavior is incorrect.

    This is all it actually takes:

    class Base {
      *[Symbol.iterator]<T>() {
        yield {} as T;
      }
    }
    
    const x = [...new Base()][0];
    //    ^? const x: T

    I tried another Symbol() there, and it works fine.

  4. Andarist commented on Jul 7, 2024

    @Andarist
    Contributor

    The proper fix for this is to thread the obtained [Symbol.iterator] signatures through resolveCall. Just like this PR does things with [Symbol.hasInstance]: #55052

    I have this working locally but it needs a lot of cleanup (implementation of iteration protocol has many layers/helper functions) and investigating all of the other ways of iterating through things. The problem is not exclusive to spreads:

    class Base {
      *[Symbol.iterator]<T>() {
        yield {} as T;
      }
    }
    
    for (const element of new Base()) {
      element;
      // ^? const element: T
    }
  5. changed the title [-]Naked generic type for this type of iterator method[/-] on Jul 8, 2024
  6. reverofevil commented on Jul 8, 2024

    @reverofevil
    Author

    Oof! It actually doesn't do resolveCall stuff at all! Not only this shouldn't compile (foo comes from where?), but if you leave only one signature, it infers correct type.

    declare class Base {
      [Symbol.iterator](foo: 1): Generator<3, void, undefined>;
      [Symbol.iterator](foo: 2): Generator<4, void, undefined>;
      [Symbol.iterator](foo: any): Generator<3 | 4, void, undefined>;
    }
    
    const x = [...new Base()];
    //    ^? const x: never[]
  7. reverofevil commented on Nov 4, 2024

    @reverofevil
    Author

    Ron Buckton (@rbuckton) Any news on this?

  8. added
    Domain: This-Typing
    on Oct 16, 2025
  9. RyanCavanaugh commented on Sep 19, 2026

    @RyanCavanaugh
    Member

    Duplicate of #38388. Both reports show that implicit Symbol.iterator calls made by spread or for...of do not instantiate the iterator method's generic this parameter, allowing that type parameter to escape into the element type.

  10. added
    Needs Human Review
    on Sep 19, 2026
  11. removed
    Needs Human Review
    on Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugDomain: This-Typing

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions