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

Downlevel Awaited #78

Closed
Newbie012 opened this issue Jun 21, 2022 · 1 comment
Closed

Downlevel Awaited #78

Newbie012 opened this issue Jun 21, 2022 · 1 comment

Comments

@Newbie012
Copy link

Typescript 4.5 has introduced the Awaited type, which recursively extracts the value inside a promise.

I wanted to contribute to this library, but I got confused about how to implement it since it calls itself:

type Awaited<T> =
    T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
        T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
            F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
                Awaited<V> : // recursively unwrap the value
                never : // the argument to `then` was not callable
        T; // non-object or non-thenable

I was able to downlevel it from:

export type K<T> = Awaited<Promise<T>>;

to

export type K<T> = T extends null | undefined ? T : T extends object & {
    then(onfulfilled: infer F): any;
} ? F extends ((value: infer V, ...args: any) => any) ? Awaited<V> : never : T;

As you can see, Awaited calls itself recursively. I tried to look for different similar types that call themselves recursively, but I found none.

I'll be happy to create a PR for this, but I'm not sure how to proceed from here.

You can see here my current attempt.

@Newbie012
Copy link
Author

Not sure why I got confused. I was probably tired...

Closing this issue in favor of #79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant