-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(trampoline): widen types, update README
This simplifies the types such that we only really care about pulling the final result out of the generator; passing around generics for arguments was not solving any problems. Updated `README.md` to be less recursion-focused; updated/fixed example. Pulled some stuff out in response to peer review
- Loading branch information
Showing
5 changed files
with
114 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,22 @@ | ||
export type AnyGenerator = Generator<any, any, any>; | ||
/** | ||
* A function type that represents a generator function for trampolining. | ||
* | ||
* @template TInitial - The type of the initial value. | ||
* @template TArg - The type of the argument passed to the thunk function. | ||
* Defaults to `TInitial`. | ||
* @template TResult - The type of the result produced by the thunk function. | ||
* Defaults to `TArg`. | ||
* @param thunk - The thunk function to be used in the generator. | ||
* @param initial - The initial value to start the generator. | ||
* @returns A generator that yields results of type `TResult`. | ||
* The final output of `asyncTrampoline()`, which will be wrapped in a `Promise`. | ||
*/ | ||
export type AsyncTrampolineGeneratorFn< | ||
TGenerator extends AnyGenerator, | ||
TArgs extends readonly any[] = unknown[], | ||
TResult = unknown, | ||
> = | ||
TGenerator extends Generator<infer T, infer TNext> | ||
? (...args: TArgs) => Generator<T, Awaited<TNext>, Awaited<TResult>> | ||
: never; | ||
export type TrampolineResult< | ||
TGeneratorFn extends (...args: any[]) => Generator = ( | ||
...args: any[] | ||
) => Generator, | ||
> = TGeneratorFn extends (...args: any[]) => Generator<any, infer TResult> | ||
? TResult | ||
: never; | ||
|
||
/** | ||
* A function type that represents a synchronous generator function for | ||
* trampolining. | ||
* | ||
* This type ensures that the result type (`TResult`) is not a `Promise`. If | ||
* `TResult` extends `Promise`, the type resolves to `never`. | ||
* | ||
* @template TArgs - The type of the arguments passed to the generator function. | ||
* Defaults to `unknown[]`. | ||
* @template TResult - The type of the result produced by the generator function. | ||
* Defaults to `unknown`. | ||
* @param args - The arguments passed to the generator function. | ||
* @returns A generator that yields results of type `TResult`. | ||
* The final output of `syncTrampoline()` | ||
*/ | ||
export type SyncTrampolineGeneratorFn< | ||
TGenerator extends AnyGenerator, | ||
TArgs extends readonly any[] = unknown[], | ||
TResult = unknown, | ||
export type SyncTrampolineResult< | ||
TGeneratorFn extends (...args: any[]) => Generator = ( | ||
...args: any[] | ||
) => Generator, | ||
> = | ||
TGenerator extends Generator<infer T, infer TNext> | ||
? (...args: TArgs) => Generator<T, TNext, TResult> | ||
: never; | ||
TrampolineResult<TGeneratorFn> extends Promise<any> | ||
? never | ||
: TrampolineResult<TGeneratorFn>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters