typescript 使用的几种情况
接口的创建
可以使用 type 和 interface 来创建类型
type 特有的优点:
- 声明基本类型别名,联合类型,元组等类型
type S = string;
type IFoo = IBar | string;
- 可使用 typeof 获取实例的类型赋值
const a:number = 1;
const IA = typeof a;
// IA 被 ts 识别为 number
interface 特有的优点
interface 能够声明合并
interface IFoo {
name:string
}
interface IFoo {
age:number
}
// 等于
type IFoo = {
name:string
age:number
}
关于对象
获取对象
以IFoo作为例子
interface IFoo {
name:string
age:number
gender:string
}
获取接口的单个属性的类型
type IBar = IFoo["name"]
// IBar = string
获取接口中一或多个属性,并将其合并为一个接口
type IBar = Pick<IFoo, "name">
// IBar = {name: string}
type IBar = Pick<IFoo, "name" | "age">
// IBar = {name: string, age: number}
忽略接口中的某些属性,将剩余属性作为一个接口
type IBar = Omit<IFoo, "name">
// IBar = {age: number, gender: string}
获取接口中所有键
type IBar = keyof IFoo
// IBar = "name" | "age" | "gender"
获取接口中所有键对应的值
type IBar = IFoo[keyof IFoo]
// IBar = string | number
创建对象
创建多个重复值的对象
type IBar = Record<"name" | "age", string>
// IBar = {name: string, age: string}
使用例子
interface IFoo {
name: string
age: string
gender: string
getSkill(): void
setSkill: (skill: string[]) => void
}
// 生成一个新类型,将 age 和 gender 的类型修改为 number,其他的类型不变
// 使用上述知识 声明一个新的高级类型IBar:
type IBar<K extends string,T = number> = (Record<K, T> & Omit<IFoo, K>)
type IBaz = IBar<"age" | "gender">
// 生成新的类型 IBaz ,符合上述描述
// 并且使用 Ibar 可将 age 和 gender (或其他)更改为任意其他类型 如:
type IBax = IBar<"age" | "gender" | "name", string[]>
关于函数
函数类型创建
创建函数类型的两种方式
interface IFoo {
name: string
age: number
gender: string
getSkill(): void // type 不支持此种声明
setSkill: (skill: string[]) => void
}
函数类型中参数的获取
以此为例子:
type IFoo = (name: string, age: number) => { name: string, age: number, gender: string }
获取函数的参数类型:
type IBar = Parameters<IFoo>
// IBar = [string, number]
获取函数的返回类型:
type IBar = ReturnType<IFoo>
// IBar = {name: string, age: number, gender: string}
typescript 使用的几种情况的更多相关文章
- Tomcat内存溢出的三种情况及解决办法分析
Tomcat内存溢出的原因 在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一样的,当然处理方式也不一样. 这里根据平时遇到的情况和相关资料进行一个总结.常见的一般会有下面三种 ...
- Objective C中数组排序几种情况的总结
总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 数组排 ...
- js内存泄露的几种情况
想解决内存泄露问题,必须知道什么是内存泄露,什么情况下出现内存泄露,才能在遇到问题时,逐个排除.这里只讨论那些不经意间的内存泄露. 一.什么是内存泄露 内存泄露是指一块被分配的内存既不能使用,又不能回 ...
- html/css基础篇——DOM中关于脱离文档流的几种情况分析
所谓的文档流,指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列.并最终窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素.脱离文档流即是元素打乱了这个排列,或是从排版中拿走. ...
- LoadRunner 场景运行error的几种情况
一. Error -27727: Step download timeout (120 seconds)has expired when downloading resource(s). Set th ...
- JS生成某个范围的随机数(四种情况)
前言: JS没有现成的函数,能够直接生成指定范围的随机数. 但是它有个函数:Math.random() 这个函数可以生成 [0,1) 的一个随机数. 利用它,我们就可以生成指定范围内的随机数. 而涉 ...
- SET Transaction Isolation Level Read语法的四种情况
转自:http://www.cnblogs.com/qanholas/archive/2012/01/04/2312152.html 存储过程:SET Transaction Isolation Le ...
- php出现“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误的一种情况,及解决方法
PHP中的“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误,可能是因为美元符号$的误用,看下面一种情况 class Test{ s ...
- 探讨read的返回值的三种情况
http://blog.chinaunix.net/uid-23629988-id-3035613.html 今天探讨一个很看似简单的API “read”的返回值问题.read的返回值有哪几个值?每个 ...
随机推荐
- JS最新最细面试题
转之:https://www.jianshu.com/p/f1f39d5b2a2e 1. javascript的typeof返回哪些数据类型. 答案:string,boolean,number,und ...
- my97datepicker实现日期改变立刻触发函数
<input type="text" class="Wdate" onclick="WdatePicker({onpicking:functio ...
- DCGAN
Deep Convolutional Generative Adversarial Networks we introduced the basic ideas behind how GANs wor ...
- oracle学习笔记(3)
使用profile文件对口令进行管理 sql>create profile 文件名 limit failed_login_arrempts 3 password_lock_time 2; 将配之 ...
- Objective-C 和 Swift 第三方库使用
https://www.jianshu.com/p/6be32a047ca7 原文地址: Objective-C 和 Swift 第三方库使用 注1:文章写于2016年9月,(swift 3.0.Xc ...
- Android自定义View——自定义ViewPager
第一部分:自定义ViewGroup的使用,手势识别器和Scroller滑动 第二部分:处理滑动监听,处理滑动冲突,增加ViewPager的指示器 常见的滑动冲突:外部滑动方向和内部滑动方向不一 ...
- Golang---sort包
Sort 包介绍 Go 语言标准库 sort 包中实现了几种基本的排序算法:插入排序.快速排序和堆排序,但是在使用 sort 包进行排序时无需具体考虑使用哪种排序方式,因为该方法会根据传入的排序的数据 ...
- redis(四)----发布订阅
发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合.pub /sub不仅仅解决发布者和订阅者直接代码级别耦合,也解决两者在物理部署上的耦合.废话不多说,直接 ...
- Cordova搭建环境与问题小结
1.Cordova介绍: Apache Cordova是一套设备API,允许移动应用的开发者使用JavaScript来访问本地设备的功能,比如摄像头.加速计.它可以与UI框架(如jQuery Mobi ...
- k8s pod.yml解释
apiVersion: v1kind: Podmetadata: name: yueying namespace: kube-public labels: name: testpodssp ...