首页 > 其他分享 >[Typescript] 120. Hard - ObjectFromEntries

[Typescript] 120. Hard - ObjectFromEntries

时间:2022-11-28 16:00:14浏览次数:41  
标签:Typescript string _____________ number 120 ObjectFromEntries Key null type

Implement the type version of Object.fromEntries

For example:

interface Model {
  name: string;
  age: number;
  locations: string[] | null;
}

type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null];

type result = ObjectFromEntries<ModelEntries> // expected to be Model
 
/* _____________ Your Code Here _____________ */

type ObjectFromEntries<T extends any[]> = {
  [Key in T as Key extends any[] ? Key[0] extends string ? Key[0]: never: never]: Key[1]
}

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

interface Model {
  name: string
  age: number
  locations: string[] | null
}

type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null]

type cases = [
  Expect<Equal<ObjectFromEntries<ModelEntries>, Model>>,
]

 

标签:Typescript,string,_____________,number,120,ObjectFromEntries,Key,null,type
From: https://www.cnblogs.com/Answer1215/p/16932429.html

相关文章

  • 【JS】120-重温基础:语法和数据类型
    最近开始把精力放在重新复习JavaScript的基础知识上面,不再太追求各种花枝招展的前端框架,框架再多,适合实际项目才是最重要。上星期在掘金发布了几篇文章,其中最大块算是 【复......
  • 基于SqlSugar的开发框架循序渐进介绍(22)-- Vue3+TypeScript的前端工作流模块中实现统一
    在工作流页面中,除了特定的业务表单信息外,往往也需要同时展示通用申请单的相关信息,因此在页面设计的时候需要使用一些组件化的概念来实现动态的内容展示处理,本篇随笔介绍Vue3......
  • [Typescript] 119. Extreme - Get Readonly Keys
    Implementageneric GetReadonlyKeys<T> thatreturnsaunionofthereadonlykeysofanObject.ForexampleinterfaceTodo{readonlytitle:stringreado......
  • typescript笔记
    letu:undefined=undefinedletn:null=null类型注解functiongreeter(person:string){return'Hello,'+person}letuser=[0,1,2]console.log(gre......
  • TypeScript学习笔记-06 类、构造器、继承、super
    //使用类来定义一个类classPerson{//readonly开头的表示只读的属性,无法进行修改//定义实例属性readonlyname:string='索隆';//静态属性......
  • TypeScript泛型
    泛型(Generic)定义一个函数或类时,有些情况下无法确定其中要使用的具体类型(返回值、参数、属性的类型不能确定),此时泛型便能够发挥作用。示例:/**在定义函数或是类时,如......
  • TypeScript属性的封装
    封装对象实质上就是属性和方法的容器,它的主要作用就是存储属性和方法,这就是所谓的封装默认情况下,对象的属性是可以任意的修改的,为了确保数据的安全性,在TS中可以对属......
  • TypeScript接口
    接口(Interface)接口的作用类似于抽象类,不同点在于接口中的所有方法和属性都是没有实值的,换句话说接口中的所有方法都是抽象方法。接口主要负责定义一个类的结构,接口可以去......
  • TypeScript抽象类
    抽象类(abstractclass)抽象类是专门用来被其他类所继承的类,它只能被其他类所继承不能用来创建实例使用abstract开头的方法叫做抽象方法,抽象方法没有方法体只能定义......
  • TypeScript之super
     示例ts文件:(function(){classAnimal{name:string;constructor(name:string){this.name=name;}sa......