首页 > 其他分享 >[Typescript] 95. Hard - Required Keys

[Typescript] 95. Hard - Required Keys

时间:2022-11-11 00:23:42浏览次数:36  
标签:Typescript _____________ Keys Required RequiredKeys Expect Key type

Implement the advanced util type RequiredKeys<T>, which picks all the required keys into a union.

For example

type Result = RequiredKeys<{ foo: number; bar?: string }>;
// expected to be “foo”

 

/* _____________ Your Code Here _____________ */

type RequiredKeys<T extends Record<PropertyKey, any>> = keyof {
  [Key in keyof T as T[Key] extends Required<T>[Key] ? Key: never]: any
}


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

type cases = [
  Expect<Equal<RequiredKeys<{ a: number; b?: string }>, 'a'>>,
  Expect<Equal<RequiredKeys<{ a: undefined; b?: undefined }>, 'a'>>,
  Expect<Equal<RequiredKeys<{ a: undefined; b?: undefined; c: string; d: null }>, 'a' | 'c' | 'd'>>,
  Expect<Equal<RequiredKeys<{}>, never>>,
]

 

标签:Typescript,_____________,Keys,Required,RequiredKeys,Expect,Key,type
From: https://www.cnblogs.com/Answer1215/p/16879310.html

相关文章

  • [Typescript] 96. Hard - Optional Keys
    Implementtheadvancedutiltype OptionalKeys<T>,whichpicksalltheoptionalkeysintoaunion. /*_____________YourCodeHere_____________*/typeOp......
  • typescript 泛型
    一、泛型与any类型的区别泛型是等待确定的占位类型,可以理解为函数的形参;所以泛型是固定的某一个类型,实例化的时候确定其实际类型any类型是顶级类型,它包括了所有的基......
  • 安全篇 - 隐式配置 KeyStore 签名信息
    LZ-Says:技术的魅力,非几句言语可表述~需要细细品味~!前言还记得,在某司对接支付,Enmmm,微信支付的时候,申请时提交的是正式证书的信息,所以想测试,只能使用正式签名才可以。问题L......
  • [Typescript] 93. Hard - Get Required
    Implementtheadvancedutiltype GetRequired<T>,whichremainsalltherequiredfieldsForexampletypeI=GetRequired<{foo:number,bar?:string}>//exp......
  • [Typescript] 94. Hard - Get Optional
    Implementtheadvancedutiltype GetOptional<T>,whichremainsalltheoptionalfieldsForexampletypeI=GetOptional<{foo:number,bar?:string}>//expe......
  • TypeScript(基础篇)day01
    一.TS介绍1.1简介ts是2012年由微软开发,在js的基础上添加了类型支持1.2优劣势优势:任何位置都有代码提示,增加效率;类型系统重构更容易;使用最新的ECMAscript语法劣势:和......
  • 记录--Uniapp + TypeScript 配置文档
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助0目标使用uniapp +TypeScript为基础栈进行小程序开发uniapp是一个使用Vue.js开发所有前端......
  • typescript 学习笔记
    前言:学习一门新的知识,首要的问题就是概念,这里记录下一.[any,unkonw]的区别any不做类型判断,可以任意[赋值,使用]1let_a:any="1";//ok2_a=1;//ok3co......
  • Leetcode第864题:获取所有钥匙的最短路径(Shortest path to get all keys)
    解题思路想到最短路径问题,自然想到用BFS解决问题,但是只记录位置还不够,还需要记录当前拥有的钥匙状态。需要的数据结构钥匙的个数是\(1-6\),用一个二进制数表示钥匙的状......
  • simpread-TypeScript 小状况之遍历对象属性 - 掘金
    本文由简悦SimpRead转码,原文地址juejin.cn在TypeScript里面,当需要遍历对象的时候,经常就会遇到下图所示的错误提示。前言最近开始在复杂项目中使用TypeScri......