首页 > 其他分享 >[Typescript] 119. Extreme - Get Readonly Keys

[Typescript] 119. Extreme - Get Readonly Keys

时间:2022-11-27 19:55:43浏览次数:36  
标签:Typescript string _____________ Get Keys title readonly type description

Implement a generic GetReadonlyKeys<T> that returns a union of the readonly keys of an Object.

For example

interface Todo {
  readonly title: string
  readonly description: string
  completed: boolean
}

type Keys = GetReadonlyKeys<Todo> // expected to be "title" | "description"
/* _____________ Your Code Here _____________ */

type GetReadonlyKeys<T> = keyof {
  [Key in keyof T as Equal<Pick<T, Key>, Readonly<Pick<T, Key>>> extends true ? Key: never]: T[Key]
}

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

type cases = [
  Expect<Equal<'title', GetReadonlyKeys<Todo1>>>,
  Expect<Equal<'title' | 'description', GetReadonlyKeys<Todo2>>>,
]

interface Todo1 {
  readonly title: string
  description: string
  completed: boolean
}

interface Todo2 {
  readonly title: string
  readonly description: string
  completed?: boolean
}

 

标签:Typescript,string,_____________,Get,Keys,title,readonly,type,description
From: https://www.cnblogs.com/Answer1215/p/16927681.html

相关文章

  • typescript笔记
    letu:undefined=undefinedletn:null=null类型注解functiongreeter(person:string){return'Hello,'+person}letuser=[0,1,2]console.log(gre......
  • GetWindowThreadProcessId
    ​​编辑本段​​一、VC--------------------------------------------------------------------------------TheGetWindowThreadProcessIdfunctionretrieves......
  • Java使用Pipeline对Redis批量读写(hmset&hgetall)
    一般情况下,RedisClient端发出一个请求后,通常会阻塞并等待Redis服务端处理,Redis服务端处理完后请求命令后会将结果通过响应报文返回给Client。这有点类似于HBase的Scan,通常......
  • TypeScript学习笔记-06 类、构造器、继承、super
    //使用类来定义一个类classPerson{//readonly开头的表示只读的属性,无法进行修改//定义实例属性readonlyname:string='索隆';//静态属性......
  • TypeScript泛型
    泛型(Generic)定义一个函数或类时,有些情况下无法确定其中要使用的具体类型(返回值、参数、属性的类型不能确定),此时泛型便能够发挥作用。示例:/**在定义函数或是类时,如......
  • TypeScript属性的封装
    封装对象实质上就是属性和方法的容器,它的主要作用就是存储属性和方法,这就是所谓的封装默认情况下,对象的属性是可以任意的修改的,为了确保数据的安全性,在TS中可以对属......
  • C++生成QML代码与QML里面集成QWidget
    目录​​1 QML代码生成​​​​2 注册机制的含义​​​​3  QWidgetInQml QML里面集成widget​​​​4 QML_OSR_EXP  将QtWidgets嵌入到QML界面中的一种示范​......
  • TypeScript接口
    接口(Interface)接口的作用类似于抽象类,不同点在于接口中的所有方法和属性都是没有实值的,换句话说接口中的所有方法都是抽象方法。接口主要负责定义一个类的结构,接口可以去......
  • TypeScript抽象类
    抽象类(abstractclass)抽象类是专门用来被其他类所继承的类,它只能被其他类所继承不能用来创建实例使用abstract开头的方法叫做抽象方法,抽象方法没有方法体只能定义......
  • TypeScript之super
     示例ts文件:(function(){classAnimal{name:string;constructor(name:string){this.name=name;}sa......