-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Naked generic type returned from iterator method #59161
Comments
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. |
Also it doesn't have to be 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 |
The proper fix for this is to thread the obtained [ 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
} |
Oof! It actually doesn't do 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[] |
@rbuckton Any news on this? |
π Search Terms
generic generator static
π Version & Regression Information
β― Playground Link
Link
π» Code
π 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
On the other hand, this is a nice way to create unique types for opaque type implementation! π
The text was updated successfully, but these errors were encountered: