当前位置:网站首页>Learning typescript (II)
Learning typescript (II)
2022-07-28 21:21:00 【InfoQ】
type inference
Joint type
let numberOrString: number | string
numberOrString.toString() // Common properties and methods of union types
Types of assertions
// union types
let numberOrString: number | string
function getLength(input: string | number): number {
const str = input as string
if (str.length) {
return str.length
} else {
const number = input as number
return number.toString().length
}
}
//type guard
function getLength2(input: string | number): number {
if (typeof input === 'string') {
return input.length
} else {
return input.toString().length
}
}
enumeration enums
// Enumeration of numbers , Enumeration members are assigned from the first value ( The default is 0) The number that starts to increase
enum Direction {
Up,
Down,
Left,
Right,
}
console.log(Direction.Up) // 0
console.log(Direction[0]) // Up
// String Enum
enum Direction {
Up = 'UP',
Down = 'DOWN',
Left = 'LEFT',
Right = 'RIGHT',
}
const value = 'UP'
if (value === Direction.Up) {
console.log('go up!')
}
// Constant enumeration , Reduce performance
const enum Direction {...}
Generic Generics
Generic basis
function echo<T>(arg: T): T {
return arg
}
const result = echo(true)
function swap<T, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]]
}
const result2 = swap(['string', 123])
Constraint generics
interface IWithLength {
length: number
}
function echoWithLength<T extends IWithLength>(arg: T): T {
console.log(arg.length)
return arg
}
const str = echoWithLength('str') // Want to have length attribute
const obj = echoWithLength({ length: 10, width: 10})
const arr2 = echoWithLength([1, 2, 3])
Class
class Queue<T> {
private data = [];
push(item: T) {
return this.data.push(item)
}
pop(): T {
return this.data.shift()
}
}
const queue = new Queue<number>()
queue.push(1)
console.log(queue.pop().toFixed())
Application in interface
interface KeyPair<T, U> {
key: T
value: U
}
let kp1: KeyPair<number, string> = { key: 1, value: "string"}
let kp2: KeyPair<string, number> = { key: 'str', value: 2 }
let arr: number[] = [1,2,3]
let arrTwo: Array<number> = [1,2,3] // Array Built in generics
边栏推荐
- 广和通&高通物联网技术开放日成功举办
- MobileViT:挑战MobileNet端侧霸主
- Jiuxin intelligence officially joined opengauss community
- Api 接口优化的几个技巧
- Niuke turns on the camera and the picture disappears a few seconds later | the picture flashes when the camera is turned on
- Unity3d tutorial notes - unity initial 03
- 牛客打开摄像头几秒后画面消失 | 相机打开画面一闪一闪
- [input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)
- 证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
- [Topic] add two numbers
猜你喜欢

quii cordova-plugin-telerik-imagepicker插件多图上传乱序

Basic operations of unity3d scene production

DeiT:注意力Attention也能蒸馏

属性基加密仿真及代码实现(CP-ABE)论文:Ciphertext-Policy Attribute-Based Encryption
![[Zhou Zhou has a prize] cloud native programming challenge](/img/0d/e26e37cddf3cf01b5e9dcaf7211106.png)
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!

Nacos 原理

Confession of a graduate student: why am I addicted to opengauss community?

云安全核心技术

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

工业通讯领域的总线、协议、规范、接口、数据采集与控制系统
随机推荐
New development of letinar in Korea: single lens 4.55G, light efficiency up to 10%
Unity3d tutorial notes - unity initial 03
在子组件中使用el-date-picker报错
Two excellent software of my love cracking, batch search text, image and video image quality enhancement
source insight 使用快捷键
怎么理解数据网格(Data Mesh)
CVPR 2022 | in depth study of batch normalized estimation offset in network
基于Xilinx的时序分析与约束
详细讲解C语言12(C语言系列)
Nacos principle
Eureka相互注册,只显示对方或只在一个中显示问题
Sharkteam completes the safety audit of flow ecological NFT market matrixmarket
Ctfshow network lost track record (2)
Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
How to build internal Wikipedia
Nacos 原理
Reading and writing basic data types in protobuf
学习Typescript(二)
Interpretation of netappp SP sensors output content
移动端空余部位自动填充