当前位置:网站首页>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
边栏推荐
- 探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
- How to build a foreign environment for the self-supporting number of express evaluation? How much does it cost?
- 一名在读研究生的自白:我为什么会沉迷于openGauss 社区?
- Maxwell 一款简单易上手的实时抓取Mysql数据的软件
- 属性基加密仿真及代码实现(CP-ABE)论文:Ciphertext-Policy Attribute-Based Encryption
- It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv
- Timing analysis and constraints based on Xilinx
- ctfshow 网络迷踪做题记录(1)
- Mobilevit: challenge the end-to-side overlord of mobilenet
- ZCMU--5066: 黑暗长廊
猜你喜欢

Young freshmen yearn for more open source | here comes the escape guide from open source to employment!

Cause analysis of restart of EMC cx4-120 SPB controller

How does lazada store make up orders efficiently? (detailed technical explanation of evaluation self-supporting number)

【TiDB】txt文档导入数据库,这样做真的很高效

一名在读研究生的自白:我为什么会沉迷于openGauss 社区?

Reading and writing basic data types in protobuf

到底为什么不建议使用SELECT * ?

属性基加密仿真及代码实现(CP-ABE)论文:Ciphertext-Policy Attribute-Based Encryption

Coding with these 16 naming rules can save you more than half of your comments!

Applet container technology improves mobile R & D efficiency by 500%
随机推荐
BUUCTF做题Upload-Labs记录pass-11~pass-20
What is ci/cd| Achieve faster and better software delivery
MySQL sorts out the review content -- with mind map
Mobilevit: challenge the end-to-side overlord of mobilenet
The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
58岁安徽人,干出瑞士今年最大IPO 投资界
MoCo V1:视觉领域也能自监督啦
微服务架构下的系统集成
Unity knowledge points summary (1)
1945. 字符串转化后的各位数字之和
工业通讯领域的总线、协议、规范、接口、数据采集与控制系统
[tidb] importing TXT documents into the database is really efficient
How to turn on or off the disk LED of EMC Vmax
Link with bracket sequence I (state based multidimensional DP)
【input 身份证号】星号 代替,input 切割成 多个 小格格(类似)
[Topic] add two numbers
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
Unit editor details
Api 接口优化的几个技巧
protobuf 中基础数据类型的读写