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

Support conditional types for Generator T / TNext / TReturn #57671

Open
6 tasks done
JAForbes opened this issue Mar 7, 2024 · 3 comments
Open
6 tasks done

Support conditional types for Generator T / TNext / TReturn #57671

JAForbes opened this issue Mar 7, 2024 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@JAForbes
Copy link

JAForbes commented Mar 7, 2024

πŸ” Search Terms

"Generator", "Async Generator", "conditional types", "const" "inference"

βœ… Viability Checklist

⭐ Suggestion

If a generator function is specifically typed to have a conditional relationship between T and TNext then type inference should allow for a type narrowed result according to that conditional type.

πŸ“ƒ Motivating Example

Typescript would be able to support complex type state management (ala redux saga), coroutines (e.g. co) and the interpreter pattern / free monad/applicative.

In the past this was not possible, but given typescript now has as const, infer and conditional types, we should be able to support typed yield result inference in key circumstances.

Something like this would be possible:

type Yielded<T> 
  T extends Promise<infer U>
   ? U // awaited
   : T  // return the original yielded value
   
function co<T>( fn: () => Generator<T, T, Yielded<T>> ) : Promise<T> {...}

πŸ’» Use Cases

  1. What do you want to use this for?

Complex state management and side effects.

  1. What shortcomings exist with current approaches?

For generator functions yielded values are always typed as any. For async generators yielded values are a union of all possible results.

  1. What workarounds are you using in the meantime?

Manually casting

doSomething(function * f(){
    let value = (yield someStream) as UnwrapStream<typeof someStream>
    let another = (yield somePromise) as UnwrapPromise<typeof somePromise>
})
@fatcerberus
Copy link

This sounds like a variant of #33912 just with generators instead of functions.

@nmain
Copy link

nmain commented Mar 7, 2024

See #43632 as well

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Mar 7, 2024
@JAForbes
Copy link
Author

This sounds like a variant of #33912 just with generators instead of functions.

Just for clarification. I don't think it is . Isn't #33912 about inferring the return type by analyzing the control flow of a function. That's now what I am suggesting.

I want to explicitly define the behavior of a given generator function with a predefined conditional type.

That way a framework can say "if you yield x, we will inject y". There is no need to analyze control flow of the function it is part of a framework's presented API.

Also I think this allows typescript to fix its lack of typing for yielded values in generators without introducing a breaking change for old code, and without requiring any new config option in tsconfig, because defining a conditional type is completely opt in.

Finally, I think this should already work based on how conditional types operate in every other context, its arguably a bug and at least surprising behavior that it doesn't.

I think #43632 is solving the same problem in a different way. But I think @Andarist had the same idea in the comment thread of that issue:

However, this is a valid criticism and maybe an alternative approach could be figured out. For instance, we could "dispatch" to the injected type using conditional types like this:
comment

I think the suggestion in #43632 is to define a YieldType in scope of the generator and then yielded values that adhere to that type will magically be inferred to be the Yield type.

My approach is that conditional types as implemented just also support yielded values. You would need to explicitly annotate the generator to use a specific instance of Generator<X,Y,Z> where X,Y,Z can all be conditional types. It wouldn't magically inject into generators in scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants