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的返回值有哪几个值?每个 ...
随机推荐
- POJ 1423:Big Number 求N的阶乘的长度 斯特林公式
Big Number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27027 Accepted: 8626 Descr ...
- 人人嫌丑的iPhoneX刘海屏为何会被手机厂商竞相模仿
不得不提到的是,苹果的iPhone自发布以来就始终引领着智能手机发展的方向.比如iPhone一代出现之后,就慢慢将键盘手机赶下历史舞台,让触屏手机成为主流.此外,iPhone的指纹识别.金属机身.玻璃 ...
- Java算法练习—— Z 字形变换
题目链接 题目描述 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- node —— 静态资源文件管理
var http = require("http"); var url = require("url"); var fs = require("fs& ...
- 【LeetCode】电话号码的字母组合
[问题]给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:" 输出:["ad ...
- BZOJ:2190: [SDOI2008]仪仗队
题解:欧拉函数 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...
- 创建Oracle序列sequence
create sequence SEQ_ID minvalue 1 maxvalue 99999999 start with 1 increment by 1 nocache order; 建解发器代 ...
- UITextFeild的基本属性
textField 基本属性 _textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textFiel ...
- JSTL与EL表达式(为空判断)
JSTL与EL表达式(为空判断) 一.循环遍历集合 1.在jsp中引入标准函数声明 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...
- git使用散记
1.从远程clone一个项目 git clone ‘项目地址’ //clone项目地 git checkout -b dev origin/dev //远程已有dev分支,新建本地dev分支与远程相对 ...