Skip to content

Naked generic type returned from iterator methodΒ #59161

Open
@reverofevil

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! πŸ™ƒ

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions