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的返回值有哪几个值?每个 ...
随机推荐
- weex 随笔
1.三大模块: <template>:样板,内容区 <style>:css <script>: js <script> export default{ ...
- MVC学生管理系统-阶段II(添加学生信息)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示 请看上一篇文章 本文是对阶段 ...
- choice接口、同花顺使用
一 choice接口使用 1.choice软件-->量化-->下载中心,下载python接口文件 EMQuantAPI_Python 2.要先绑定手机号,绑定后账户权限不够,暂时放弃. 二 ...
- python3.4+Django+pymysql
pip install Pymysql 修改app里面的__init__.py import pymysqlpymysql.install_as_MySQLdb()
- ZOJ 2301/HDU 1199 线段树+离散化
给这个题目跪了两天了,想吐简直 发现自己离散化没学好 包括前一个离散化的题目,实际上是错了,我看了sha崽的博客后才知道,POJ那题简直数据弱爆了,本来随便一组就能让我WA掉的,原因在于离散化的时候, ...
- 实验吧web--易--后台登陆
题目地址:http://www.shiyanbar.com/ctf/2036 这道题确实有点考研脑洞了. 1.首先,查看网页源代码(Ctrl+U),会发现一段PHP代码: $sql = "S ...
- pycharm实用技巧
https://mp.weixin.qq.com/s/-48vU9KtnInFaYJ6rQ9n-w
- 51nod A 魔法部落(逆元费马小定理)
A 魔法部落 小Biu所在的部落是一个魔法部落,部落中一共有n+1个人,小Biu是魔法部落中最菜的,所以他的魔力值为1,魔法部落中n个人的魔法值都不相同,第一个人的魔法值是小Biu的3倍,第二个人的魔 ...
- ftp主动和被动模式区别
转载自:http://www.west999.com/cms/wiki/server/2018-11-16/49417.html FTP是基于TCP的服务的,FTP不同之处在于FTP使用两个端口,一个 ...
- PAT 2011 秋
A : World Cup Betting #include <cstdio> #include <iostream> #include <algorithm> u ...