frontend -- Typescript
类 · TypeScript中文网 · TypeScript——JavaScript的超集 (tslang.cn)
Typescript和Javascript的区别?
Typescript面试题 - 知乎 (zhihu.com)
1、TS变量类型:(97条消息) TS01 : TS变量类型详解TS函数_七月是我的生日的博客-CSDN博客_ts变量类型
2、TS 中那些有用的符号
细数 TS 中那些奇怪的符号_奇舞周刊的博客-CSDN博客
(1)! 非空断言操作符
忽略 undefined 和 null 类型
function myFunc(maybeString: string | undefined | null) {
// Type 'string | null | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
const onlyString: string = maybeString; // Error
const ignoreUndefinedAndNull: string = maybeString!; // Ok
}
调用函数时忽略 undefined 类型
type NumGenerator = () => number;
function myFunc(numGenerator: NumGenerator | undefined) {
// Object is possibly 'undefined'.(2532)
// Cannot invoke an object which is possibly 'undefined'.(2722)
const num1 = numGenerator(); // Error
const num2 = numGenerator!(); //OK
}
因为 !
非空断言操作符会从编译生成的 JavaScript 代码中移除,所以在实际使用的过程中,要特别注意。比如下面这个例子:
const a: number | undefined = undefined;
const b: number = a!;
console.log(b);
以上 TS 代码会编译生成以下 ES5 代码:
"use strict";
const a = undefined;
const b = a;
console.log(b);
虽然在 TS 代码中,我们使用了非空断言,使得 const b: number = a!;
语句可以通过 TypeScript 类型检查器的检查。但在生成的 ES5 代码中,!
非空断言操作符被移除了,所以在浏览器中执行以上代码,在控制台会输出 undefined
(2)?. 可选链操作符
可选的属性访问:const val = a?.b;
可选元素访问:function tryGetArrayElement<T>(arr?: T[], index: number = 0) {
return arr?.[index];
}
可选链与函数调用:let result = obj.customMethod?.();
(3)?? 空值合并运算符(可返回 falsy 值(''、NaN 或 0))
??和 | | 的区别:
使用 ??
时,只有当值1为null
或undefined
时才返回值2;
使用 ||
时,值1会转换为布尔值判断,为true
返回值1,false
返回值2
const foo = null ?? 'default string';
console.log(foo); // 输出:"default string"
const baz = 0 ?? 42;
console.log(baz); // 输出:0
不能与 && 或 || 操作符共用:这种情况下会抛出 SyntaxError。
(4)?: 可选属性
interface Person {
name: string;
age?: number;
}
let lolo: Person = {
name: "lolo"
}
(5)& 运算符:交叉类型
通过 &
运算符可以将现有的多种类型叠加到一起成为一种类型,它包含了所需的所有类型的特性。
type PartialPointX = { x: number; };
type Point = PartialPointX & { y: number; };
let point: Point = {
x: 1,
y: 1
}
3、类型别名
(1)TypeScript 提供了为类型注解设置别名的便捷语法
type Pet = 'cat' | 'dog';
let pet: Pet;
pet = 'cat'; // Ok
pet = 'dog'; // Ok
pet = 'zebra'; // Compiler error
interface Person {
name: string;
age: number;
}
const sem: Person = { name: 'semlinker', age: 30 };
type Sem= typeof sem; // -> Person
(2)Partial<T> & Required<T>
TypeScript 内置的工具类型 Partial<T>
可以快速把某个接口类型中定义的属性变成可选的:
interface PullDownRefreshConfig {
threshold: number;
stop: number;
}
/**
* type PullDownRefreshOptions = {
* threshold?: number | undefined;
* stop?: number | undefined;
* }
*/
type PullDownRefreshOptions = Partial<PullDownRefreshConfig>
Required<T>可以
把某个接口中定义的属性全部声明为必选的:
interface Props {
a?: number;
b?: string;
}
const obj: Props = { a: 5 }; // OK
const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing
(3)Readonly
Readonly<T>
的作用是将某个类型所有属性变为只读属性,也就意味着这些属性不能被重新赋值。
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: "Delete inactive users"
};
todo.title = "Hello"; // Error: cannot reassign a readonly property
(4)keyof 操作符:提取interface
、type
、class
的key
//基础使用
interface Person {name: string;age: number;location: string;
}
type K1 = keyof Person; // "name" | "age" | "location"// 读取对象的属性,并且限制了只能读取对象中存在的属性
function prop<T extends object, K extends keyof T>(obj: T, key: K) {return obj[key];
}
在以上代码中,我们使用了 TypeScript 的泛型和泛型约束。首先定义了 T 类型并使用 extends
关键字约束该类型必须是 object 类型的子类型,然后使用 keyof
操作符获取 T 类型的所有键,其返回类型是联合类型,最后利用 extends
关键字约束 K 类型必须为 keyof T
联合类型的子类型。
(5)typeof 和 keyof 操作符
在 TypeScript 中,typeof
操作符可以用来获取一个变量或对象的类型。而 keyof
操作符可以用于获取某种类型的所有键,其返回类型是联合类型。了解完 typeof
和 keyof
操作符的作用,我们来举个例子,介绍一下它们如何结合在一起使用:
const COLORS = {red: 'red',blue: 'blue'
}// 首先通过typeof操作符获取Colors变量的类型,然后通过keyof操作符获取该类型的所有键,
// 即字符串字面量联合类型 'red' | 'blue'
type Colors = keyof typeof COLORS
let color: Colors;
color = 'red'// Ok
color = 'blue'// Ok// Type '"yellow"' is not assignable to type '"red" | "blue"'.
color = 'yellow'// Error
(6) Exclude
Exclude<T, U>
的作用是将某个类型中属于另一个的类型移除掉。
如果 T
能赋值给 U
类型的话,那么就会返回 never
类型,否则返回 T
类型。最终实现的效果就是将 T
中某些属于 U
的类型移除掉。
type T0 = Exclude<"a" | "b" | "c", "a">; // "b" | "c"
type T1 = Exclude<"a" | "b" | "c", "a" | "b">; // "c"
type T2 = Exclude<string | number | (() => void), Function>; // string | number
(7) Extract
Extract<T, U>
的作用是从 T
中提取出 U
。
type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a"
type T1 = Extract<string | number | (() => void), Function>; // () =>void
4、类型提取
type Person = {name: string;age: number;
}type PersonName = Person["name"]; // string
type StrNumTuple = [string, number];
type StrNumTuple0 = StrNumTuple[0]; // string
5、interface和type
typeScript interface和type区别 - 简书 (jianshu.com)
TS系列篇|接口(interface) 和 类型别名(type) - 掘金 (juejin.cn)
6、 高级类型Record
(164条消息) typescript中高级类型Record_问白的博客-CSDN博客_record ts
7、装饰器及应用场景浅析
JELLY | Typescript 装饰器及应用场景浅析 (jd.com)
frontend -- Typescript相关推荐
- Announcing TypeScript 3.7 RC,Javascriptc China
Announcing TypeScript 3.7 RC October 24th, 2019 We're pleased to announce TypeScript 3.7 RC, the rel ...
- c# typescript_在任何IDE中从C#,Java或Python代码获取TypeScript接口的简单方法
c# typescript by Leonardo Carreiro 莱昂纳多·卡雷罗(Leonardo Carreiro) 在任何IDE中从C#,Java或Python代码获取TypeScript接 ...
- TypeScript 发布 3.4 首个 RC 预览版
开发四年只会写业务代码,分布式高并发都不会还做程序员? >>> TypeScript 3.4 首个候选版本已发布.由版本号可知,这是一次较为重要的版本升级.所以我们不妨看一下有哪 ...
- TypeScript 3.9 正式发布!平均编译时长从 26 秒缩短至 10 秒
作者 | 微软官方博客 译者 | 核子可乐 策划 | 小智 稿源 | 前端之巅 今天,微软在其官方博客宣布:TypeScript 3.9 版本已经正式发布,详情见下文. 有些朋友可能对 TypeScr ...
- 如何使用React,TypeScript和React测试库创建出色的用户体验
I'm always willing to learn, no matter how much I know. As a software engineer, my thirst for knowle ...
- 使用Typescript和React的最佳实践
by Christopher Diggins 克里斯托弗·迪金斯(Christopher Diggins) 使用Typescript和React的最佳实践 (Best practices for us ...
- 快速转 TypeScript 指南
From:https://segmentfault.com/a/1190000040582994 TypeScript 教程:https://www.runoob.com/typescript/ts- ...
- pomelo + vscode + typescript搭建可约束可调试的游戏服务端框架
说在前面 pomelo: 它是网易开源的一套基于Node.js的游戏服务端框架,详情请戳这里关于pomelo的种种这里不详细说.点击链接查看详情.但是由于pomelo是js项目,使用起来的时候并不是很 ...
- typescript项目_如何设置TypeScript项目
typescript项目 by David Piepgrass 由David Piepgrass 如何设置TypeScript项目 (How to set up a TypeScript projec ...
最新文章
- 以及其任何超类对此上下文都是未知的_浏览器原理系列 - JS执行上下文详解(一):作用域
- sqlite库——c语言实现匹配已知字符串中某个字段(该字段在其他表中),在其他表中获取值并显示
- LLVM 与 Clang 介绍 — LinuxTOY
- SQLServer中的数据类型
- tableau可视化数据分析60讲(二十二)-tableau常见面试题目
- idea打开项目慢怎么办?
- 超4000人参加源码共读,喊你来一起学习成长~打开新世界
- 破五唯后,高校从“唯论文”变成了“唯纵向”?​
- 4.熟悉App Inventor 2编程界面
- Snort里如何将一个tcpdump格式的二进制文件读取打印到屏幕上(图文详解)
- setw()函数使用_什么是C++ setw() 函数?
- C# WPF网络实时监测客户端
- 图像处理笔记1一上采样与下采样
- CS 61A Spring 2019 HW02 学习笔记
- python写出租车计费系统_用VHDL设计出租车计费系统
- 青出于蓝而胜于蓝 — Vue.js对Angular.js的那些进步
- DA14580 RW消息事件处理----转
- python新年贺卡_写个新年贺卡生成器,各位小伙伴们新年快乐呀~
- linux下安装hadoop步骤
- 电子商城数据库设计思路