-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
yield operator in generator functions has any return type #26959
Comments
yield
operator in generator functions has any
return type
Can you provide an example? I've been using yield on generators - you specify the return type of the function as export function* findParents(element: HTMLElement): IterableIterator<HTMLElement> {
if (!element) {
return undefined;
}
let parent = element.parentElement;
while (parent) {
yield parent;
parent = parent.parentElement;
}
} |
I think this issue is that a function* f() {
const x = yield 0; // x is any
} |
@andy-ms ah now I understand. In that case, my understanding is this point needs to be addressed:
But this point does not (see proof below):
I'd like to add this point as well:
Given this example playground: function* f() {
const x = yield 0;
const y = yield* [2, 3, 4];
}
|
@gwicksted The value of a |
@andy-ms please forgive my ignorance! I hadn't used that syntax before today. I assumed (incorrectly) that Other than analyzing the all the consumers of the iterator looking for each call to so instead of just: interface Iterator<T> {
next(value?: any): IteratorResult<T>;
}
interface IterableIterator<T> { } You could have: interface Iterator<T,TNext = any> {
next(value?: TNext): IteratorResult<T>; // IteratorResult may also need to change?
}
interface IterableIterator<T,TNext = any> { } Then define your generator as: function* f(): IterableIterator<number, string> {
const str = yield 5; // str is string
}
const iter = f();
const num = iter.next("hello").value; // next's argument is now type checked Then TS needs to auto type yield's return type to the 2nd generic parameter of the generator's return type. Shouldn't be too difficult since typing of yield's parameter is already done via the 1st generic parameter of the generator's return type. Of course there are more advanced cases where multiple yield expressions are being passed different types but I think those are even more rare and they could simply use a union of all types expected. |
@gwicksted examples added (with your suggestion) @andy-ms great suggestions I think this could be an opt-in type refinement for case where typing on generators is needed on values supplied into it. |
@freddi301 this type definition uses the recently added default generic type notation to make it an opt-in feature without breaking compatibility anywhere. If you don't provide the 2nd type arg, it defaults to interface IterableIterator<T,TNext = any> { } |
An interesting way to support typed " function* foo(a: boolean) {
// The `b` and `c` variables must be typed
const b: number = yield 0
const c: string = yield b == 0 ? 0 : 1
}
// The inferred generator type
type Foo = (a: boolean) => {
next(value: number | string): IteratorResult<0 | 1>
return(): { value: undefined, done: true }
throw(e?: any): { value: undefined, done: true }
}
typeof foo extends Foo // => true
let gen = foo(true)
gen.next() // [ts] Expected a number|string, got undefined If Javascript had immutable generators built-in, this would probably be easier. I'm trying to get this library working with Typescript, but I don't think I can until "yield returns" can be typed. |
related: #2983 |
My proposal for generator type definition: #2983 (comment) |
For anyone interested, the issues and ideas raised here were covered in quite some detail in the comment thread in #2873. |
Why the maintainers just happily closed the issue if it was addressed only by 20%? |
Search Terms
Suggestion
yield
operatoryield*
operatorUse Cases
better type safety
redux-saga
koa.js
Example
now:
desired:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: