huihuilang53 / ts-challenges

ts类型体操刷题总结

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Myawait

huihuilang53 opened this issue · comments

commented

/*
189 - Awaited

by Maciej Sikora (@maciejsikora) #简单 #promise #built-in

题目

假如我们有一个 Promise 对象,这个 Promise 对象会返回一个类型。在 TS 中,我们用 Promise 中的 T 来描述这个 Promise 返回的类型。请你实现一个类型,可以获取这个类型。

例如:Promise<ExampleType>,请你返回 ExampleType 类型。

type ExampleType = Promise<string>

type Result = MyAwaited<ExampleType> // string

这个挑战来自于 @maciejsikora 的文章:original article

在 Github 上查看:https://tsch.js.org/189/zh-CN
*/

/* _____________ 你的代码 _____________ */

type MyAwaited<T extends PromiseLike< any >> = T extends PromiseLike< infer x> ? x extends Promise<unknown> ? MyAwaited<x> : x : never

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type X = Promise
type Y = Promise<{ field: number }>
type Z = Promise<Promise<string | number>>
type Z1 = Promise<Promise<Promise<string | boolean>>>
type T = { then: (onfulfilled: (arg: number) => any) => any }

type cases = [
Expect<Equal<MyAwaited, string>>,
Expect<Equal<MyAwaited, { field: number }>>,
Expect<Equal<MyAwaited, string | number>>,
Expect<Equal<MyAwaited, string | boolean>>,
Expect<Equal<MyAwaited, number>>,
]

// @ts-expect-error
type error = MyAwaited

/* _____________ 下一步 _____________ /
/

分享你的解答:https://tsch.js.org/189/answer/zh-CN
查看解答:https://tsch.js.org/189/solutions
更多题目:https://tsch.js.org/zh-CN
*/