00189-easy-awaited

Back

type MyAwaited<T> = T extends PromiseLike<infer F>
  ? MyAwaited<F>
  : T

Solution by AleksandrShcherbackov #36144

type MyAwaited<T extends Promise<any>> = T extends Promise<infer U>
  ? U extends Promise<any>
    ? MyAwaited<U>
    : U
  : never;

Solution by KimKyuHoi #36134

简单翻了一下答案,似乎清一色的都是下面这个答案

type MyAwaited<T extends PromiseLike<any>> = T extends PromiseLike<infer K>
  ? K extends PromiseLike<any>
    ? MyAwaited<K>
    : K
  : never;

但是 TS 原本的 Awaited 是支持非 PromiseLike 类型的,即 Awaited<string> 就是 string 虽然测试用例并没有对这一项进行测试

所以下面这种应该是更接近原版功能的实现?即不满足返回 T,且接受任何类型

type MyAwaited<T> = T extends PromiseLike<infer K> 
  ? K extends PromiseLike<any> 
    ? MyAwaited<K> 
    : K 
  : T

Solution by h4nabii #36087

type MyAwaited<T> = T extends PromiseLike<infer Type> ? (Type extends PromiseLike<any> ? MyAwaited<Type> : Type) : T

Solution by karazyabko #36028

// 你的答案
type MyAwaited<T extends PromiseLike<any | PromiseLike<any>>> = T extends PromiseLike<infer R> ? R extends PromiseLike<any> ? MyAwaited<R> : R : null

Solution by reonce #36009

type MyAwaited<T> = T extends PromiseLike<infer U> ? MyAwaited<U> : T;

Solution by chen8840 #35947

type MyAwaited<T> = T extends Promise<infer I> 
 ? MyAwaited<I> 
 : T extends { then: (onfulfilled: (arg: infer Arg) => any) => any } 
 ? Arg 
 : T

type MyAwaited<T> = T extends PromiseLike<infer Res> 
 ? Res extends PromiseLike<any> 
 ? MyAwaited<Res> 
 : Res 
 : T

Solution by EvilEl #35871

// 你的答案
type MyAwaited<T> = T extends PromiseLike<infer K> ? MyAwaited<K> : T

Solution by song4613107 #35790

type MyAwaited<T> = 
  T extends PromiseLike<infer Res>
    ? Res extends PromiseLike<any>
      ? MyAwaited<Res>
      : Res
    : T

Solution by XenoPOMP #35741

// 你的答案
type MyAwaited<T extends Promise<unknown>> = T extends Promise<infer K>
	? K
	: never

Solution by ndwgg #35544

type MyAwaited<T extends PromiseLike<any>> = T extends PromiseLike<infer P> ? P extends PromiseLike<unknown> ? MyAwaited<P> : P : never;

Solution by gangnamssal #35479

type MyAwaited<T> = T extends PromiseLike<infer U> ? MyAwaited<U> : T;

Solution by RanungPark #35436

type MyAwaited<T> = T extends PromiseLike<infer U> ? MyAwaited<U> : T;

Solution by RanungPark #35435

type MyAwaited<T> = T extends { then: (onfulfilled: (arg: infer R) => any) => any } ? MyAwaited<R> : T;

Solution by sesolaga #35385

type MyAwaited<T> = T extends PromiseLike<infer U> ? MyAwaited<U> : T

Solution by gyeounjeong #35357

// 你的答案
type MyAwaited<T> = T extends Promise<infer R> ? R extends Promise<infer M> ? MyAwaited<R> : R : T extends {
  then:(arg1:(arg2:infer Q)=>any,...args:any)=>any} ? Q :never

Solution by front-end-Dean #35268

type MyAwaited<T> = T extends PromiseLike<infer U> ? MyAwaited<U> : T

I’m not sure if this is correct, but the test cases didn’t report any errors.

Solution by kid1110 #35178

// 你的答案
type Thenable<T> = {
  then: (onfulfilled: (arg: T) => unknown) => unknown;
}

type MyAwaited<T> = T extends Promise<infer Inner> | Thenable<infer Inner> ? MyAwaited<Inner> : T

Solution by HoseaGuo #35127

type MyAwaited<T> = T extends Promise<infer P> ? P : never

Solution by ClarityOfMind #34988

type MyAwaited<T> = T extends Promise<infer U> ? U : T;

Solution by raeyoung-kim #34939

type MyAwaited = T extends PromiseLike ? MyAwaited

: T;

no so complex,just this!

Solution by shx123qwe #34928

Answers: type MyAwaited<T> = T extends Promise<infer R> ? MyAwaited<R> : T extends { then: (onfulfilled: (arg: infer U) => any) => any } ? U : T; 

Understanding the MyAwaited Type Utility in TypeScript
Introduction
In TypeScript, working with asynchronous code often involves using promises. When dealing with promises, it can be helpful to extract the type that a promise resolves to. This is where a utility type like MyAwaited comes into play. MyAwaited allows developers to retrieve the resolved type of a promise, even when dealing with nested promises. This article will explore the implementation and functionality of MyAwaited.

Purpose of MyAwaited
The primary goal of the MyAwaited type utility is to take a promise type and return the type that it resolves to. Additionally, MyAwaited can handle promises that are nested within other promises and can also work with objects that resemble promises, such as those with a then method.

Implementation of MyAwaited
Here’s the implementation of the MyAwaited type utility:

type MyAwaited<T> = 
  T extends Promise<infer R>
    ? MyAwaited<R> // Recursive check for nested promises
    : T extends { then: (onfulfilled: (arg: infer U) => any) => any }
      ? U  // Handling objects with a `then` method
      : T; // Return T if not a promise or similar object

How It Works
Recursive Handling of Nested Promises:

The type utility begins by checking if T is a promise using the conditional type T extends Promise<infer R>.
If T is indeed a promise, it uses infer R to extract the resolved type R of that promise.
Then, MyAwaited<R> is invoked recursively to check if R itself is a promise, allowing the utility to navigate through any nested promises until it finds the ultimate resolved type.
Handling Promise-like Objects:

The second conditional check, T extends { then: (onfulfilled: (arg: infer U) => any) => any }, allows MyAwaited to work with objects that implement a then method, resembling a promise.
If T meets this condition, it infers the type U from the argument of the then method and returns it.
Default Case:

If T is neither a promise nor an object with a then method, it simply returns T

Solution by ESGAntall #34910

type MyAwaited<T extends PromiseLike<any>> = 
  T extends PromiseLike<infer U> 
    ? U extends PromiseLike<any>
      ? MyAwaited<U>
      : U
    : never; // Since there is a constraint with using PromiseLike interface, no need to return T type here. 

Solution by highspirit7 #34901

type MyAwaited<T> = T extends PromiseLike<infer P> ? (P extends PromiseLike<any> ? MyAwaited<P> : P): never

Solution by nguyste #34855

type MyAwaited<T> = T extends Promise<infer R> 
  ? MyAwaited<R> 
  : T extends { then: ( onfulfilled: (arg: infer D) => any) => any } 
    ? D 
    : T

Solution by eunsukimme #34812

type MyAwaited<T> = 
  T extends PromiseLike<infer R> 
    ? R extends PromiseLike<any> 
      ? MyAwaited<R> 
      : R 
    : never
image

Solution by Git-I985 #34702

// your answers
T 需要符合 PromiseLike 接口的类型约束
type MyAwaited<T extends PromiseLike<any>> = T extends PromiseLike<infer R>
  ? R extends PromiseLike<any>
    ? MyAwaited<R>
    : R
  : never;

Solution by zeyuanHong0 #34691

type MyAwaited<T> = T extends PromiseLike<infer S> ? S extends PromiseLike<any> ? MyAwaited<S> : S : never;

Solution by nathan2slime #34659

type MyAwaited<T> = Awaited<T>

Solution by Hailey0930 #34636

// your answers
type MyAwaited<T> = T extends PromiseLike<infer U> ? MyAwaited<U> : T;

Solution by pwn0x80 #34585