首页 > 其他分享 >[Typescript] 117. Hard - ClassPublicKeys

[Typescript] 117. Hard - ClassPublicKeys

时间:2022-11-26 16:23:45浏览次数:40  
标签:Typescript _____________ ClassPublicKeys getNum 117 bool str type

Implement the generic ClassPublicKeys<T> which returns all public keys of a class.

For example:

class A {
  public str: string
  protected num: number
  private bool: boolean
  getNum() {
    return Math.random()
  }
}

type publicKyes = ClassPublicKeys<A> // 'str' | 'getNum'

 

/* _____________ Your Code Here _____________ */

type ClassPublicKeys<C> = keyof C


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

class A {
  public str: string
  protected num: number
  private bool: boolean
  constructor() {
    this.str = 'naive'
    this.num = 19260917
    this.bool = true
  }

  getNum() {
    return Math.random()
  }
}

type cases = [
  Expect<Equal<ClassPublicKeys<A>, 'str' | 'getNum'>>,
]

 

标签:Typescript,_____________,ClassPublicKeys,getNum,117,bool,str,type
From: https://www.cnblogs.com/Answer1215/p/16927640.html

相关文章

  • TypeScript学习笔记-04 tsconfig.json配置文件
    tsconfig.json一般常用的配置如下所示,可以按需要进行配置。{/*tsconfig.json是ts编译器的配置文件,ts编译器可以根据他的信息来对代码进行编译//in......
  • TypeScript编译选项
    编译选项自动编译文件编译文件时,使用-w指令后,TS编译器会自动监视文件的变化,并在文件发生变化时对文件进行重新编译。但是一次只能编译一个文件。示例:  tsc......
  • TypeScript学习笔记-03 TS基础语法
    即使有错还是可以编译为JS文件。ts方法指定参数的类型,指定返回值的类型:这样都会对返回值的类型和参数都会进行校验!......
  • TypeScript学习笔记-01TS是什么?
      TypeScript简称TS,是微软公司设计的一门语言。以JavaScript为基础构建的语言,扩展了JS、兼容JS(甚至可以在TS文件中兼容使用JS)、并且添加了类型,并且可以在任何支持JavaSc......
  • TypeScript--高级用法
    TypeScript--高级用法1.运算符可选链运算符?.判断左侧的表达式是否是null或者undefined,如果是,则会停止表达式的运行,减少我们大量的&&运算obj?.propobj?.[i......
  • TypeScript - -类型实战
    TypeScript--类型实战下面介绍的几个常见实战操作,数量不多,但是提供了一些思路,学习理解这些思路,和js实现的区别。为自己写代码的时候打下小小的基础1.实现返回promi......
  • TypeScript--5常见问题
    TypeScript--5常见问题interface和type的区别是什么||interface|type||---|---|---||可定义类型|object,array,function,classconstructor|所......
  • [Typescript] 115. Hard - Drop String
    Dropthespecifiedcharsfromastring.Forexample:typeButterfly=DropString<'foobar!','fb'>//'ooar!' /*_____________YourCodeHere_____________......
  • TypeScript类型(二)
    对象 示例://#regionjs写法//object表示一个js对象leta:object;a={};a=function(){};//#endregion//#regionTypeScript写法//{}用来指定对......
  • TypeScript类型声明
    基本类型类型声明类型声明是TS非常重要的一个特点通过类型声明可以指定TS中变量(参数、形参)的类型指定类型后,当为变量赋值时,TS编译器会自动检查值是否符合类型......