首页 > 其他分享 >ts类型体操 Awaited

ts类型体操 Awaited

时间:2023-04-16 10:01:42浏览次数:34  
标签:https _____________ ts Awaited number Promise Expect 体操 type


/*
  189 - Awaited
  -------
  by Maciej Sikora (@maciejsikora) #easy #promise #built-in

  ### Question

  If we have a type which is wrapped type like Promise. How we can get a type which is inside the wrapped type?

  For example: if we have `Promise<ExampleType>` how to get ExampleType?

  ```ts
  type ExampleType = Promise<string>

  type Result = MyAwaited<ExampleType> // string
  ```

  > This question is ported from the [original article](https://dev.to/macsikora/advanced-typescript-exercises-question-1-45k4) by [@maciejsikora](https://github.com/maciejsikora)

  > View on GitHub: https://tsch.js.org/189
*/

/* _____________ Your Code Here _____________ */


// your answers



// your answers
type MyAwaited<T> = T extends Promise<infer P> ? P extends Promise<any> ? MyAwaited<P> : P : never;





/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type X = Promise<string>
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<X>, string>>,
  Expect<Equal<MyAwaited<Y>, { field: number }>>,
  Expect<Equal<MyAwaited<Z>, string | number>>,
  Expect<Equal<MyAwaited<Z1>, string | boolean>>,
  Expect<Equal<MyAwaited<T>, number>>,
]

// @ts-expect-error
type error = MyAwaited<number>

/* _____________ Further Steps _____________ */
/*
  > Share your solutions: https://tsch.js.org/189/answer
  > View solutions: https://tsch.js.org/189/solutions
  > More Challenges: https://tsch.js.org
*/

标签:https,_____________,ts,Awaited,number,Promise,Expect,体操,type
From: https://blog.51cto.com/u_15283585/6193331

相关文章

  • heatmapts_simple-heatmap的使用
    simpleheat的使用<scriptsetuplang="ts">import{SimpleHeat}from"simpleheat-ts";import*asdatfrom"dat.gui";letframe:number|null=null;constoCanvas=document.createElement("canvas");oCanva......
  • vs 2017编译bootst库
    1.下载boost源码,这里下载boost_1_69_0:boost_1_69_0.7z.2.解压后,已管理员身份打开VS2017的x86_x64兼容工具.例如我的路径是F:F://切换到F盘cdwork\C++\boost_1_69_0执行bootstrap.bat生成b2.exe执行b2.exe--toolset=msvc-14.1install--prefix="D:\Project\Boos......
  • Python模块-requests
    1、requests模块requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求。功能强大,用法简洁高效。2、模块介绍及请求过程requests模块模拟浏览器发送请求请求流程:指定url-->发起请求-->获取响应对象中存储的数据-->持久化存储3、爬取百度首页#!......
  • Default Arguments总结
    默认实参默认实参在C++编程实践中非常常见,但其中也有一些有趣的知识点与扩展,本文对默认实参做简单总结;函数默认实参默认实参用来取代函数调用中缺失的尾部实参:voidProcess(intx=3,inty=4){}拥有默认实参的形参之后的形参必须在同一作用域内有声明提供了默认参数,否则编译......
  • C语言的fgets函数
    fgets是C语言中的一个标准库函数,用于从指定文件中读取一行字符串。它的声明如下:char*fgets(char*str,intn,FILE*stream);其中,str是一个字符数组,用来存储读取的字符串;n表示读取的最大字符数(包括换行符和终止符);stream表示要读取的文件流。fgets函数会从stream中读取字符,直到遇......
  • Python Web全栈工程师「课代表itspcool」
    PythonWeb全栈工程师核心代码,注释必读//下、栽、课、呈茄/:itspcool进入Python世界,打开编程之门Python语法基础一、标识符所谓的标识符就是对变量、常量、函数、类等对象起的名字。首先必须说明的是,Python语言在任何场景都严格区分大小写!Python对于标识符的命名......
  • D. Connected Components
    D.ConnectedComponentshttps://www.codeforces.com/contest/292/problem/D 思路由于需要删除任意连续段的连接线,引入前缀和连续段的左右两边都需要,所以引入两个前缀和。 https://blog.csdn.net/qq_28954601/article/details/79281640Codehttps://blog.csdn.net/q......
  • 67、K8S-部署管理-Helm部署Prometheus、TSDB数据持久化
    Kubernetes学习目录1、准备仓库1.1、配置prometheus仓库1.1.1、增加prometheus仓库helmrepoaddprometheus-communityhttps://prometheus-community.github.io/helm-charts1.1.2、查询增加的结果]#helmrepolistNAMEURL......
  • 前端pdf一次下载多个echarts图表
    //需下载pdf,html2canvas模块savePDF:function(){letobj1=document.getElementById("pieChart");letobj2=document.getElementById("homepage-bandwidth-container");letobj3=document.getElementById("homepage-iops-container");le......
  • ContextSwitch 学习与使用
    ContextSwitch学习与使用说明github上面有一个简单的测试系统调用以及上下文切换的工具.contextswitch.下载之后直接make就可以进行简单的测试需要注意的是部分arm环境没有:-mno-avx这个参数,需要去掉一下.官方文档以及说明Littlemicro-benchmarkstoassess......