Typescript 指北
官方文档
https://www.tslang.cn/docs/handbook/basic-types.html
快速开始
安装
1
| npm install -g typescript
|
编译
查看版本
基础类型
布尔值
1
| let isDone: boolean = false;
|
数字
1 2 3 4
| let decLiteral: number = 6; let hexLiteral: number = 0xf00d; let binaryLiteral: number = 0b1010; let octalLiteral: number = 0o744;
|
模板字符串
1 2 3 4
| let name: string = "bob"; let age: number = 37; let sentence: string = `Hello, my name is ${ name }. I'll be ${ age + 1 } years old next month.`;
|
数组
1 2 3
| let list: number[] = [1, 2, 3]; let list: Array<number> = [1, 2, 3];
|
元组 Tuple
1 2 3
| let x: [string, number]; x = ["hello", 10]; // OK x = [10, "hello"]; // Error
|
枚举
1 2
| enum Color {Red, Green, Blue} let c: Color = Color.Green;
|
Any
1 2 3
| let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean
|
Void
1 2 3
| function warnUser(): void { console.log("This is my warning message"); }
|
Null 和 Undefined
1 2
| let u: undefined = undefined; let n: null = null;
|
Never
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| // 返回never的函数必须存在无法达到的终点 function error(message: string): never { throw new Error(message); }
// 推断的返回值类型为never function fail() { return error("Something failed"); }
// 返回never的函数必须存在无法达到的终点 function infiniteLoop(): never { while (true) { } }
|
接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| interface Parent { name: string age: number }
interface child extends Parent { address: string }
const jibao:child = { name: 'wangjingdong', age: 5, address: 'xi`an' }
console.log(jibao.name)
|
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Article { public title: string // 公共 public detail?: string readonly author: string | number = 'Jonh' // 只读 // private intro?: string // 私有 protected protectedData?: string // 内部受保护的 // static 静态,是给类的本身,而不是类的实例 // private protected privateProtectedData: string = 'privateProtectedData' constructor (title: string, detail: string ) { this.title = title this.detail = detail } }
const bookA = new Article('传奇', '在那段战火纷飞,饱受苦难的年代,有一位伟大的地主家儿子......') console.log(bookA.author)
class Mao extends Article { public role: string constructor (title: string, detail: string, role: string) { super(title, detail) this.role = role } }
const mao = new Mao('传奇', '在那段战火纷飞,饱受苦难的年代,有一位伟大的地主家儿子......', 'Mr mao') console.log(mao)
|
存储器属性
1 2 3 4 5 6 7 8 9 10 11 12
| class User { private _password: string = '' get password (): string { return '******' // this._password } set password (value: string) { this._password = value } }
const userA = new User() console.log(userA.password)
|
抽象类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <blockquote>用来作为子类基类去使用,规范格式的</blockquote> abstract class Animal { name: string constructor (name: string) { this.name = name } abstract sayHi (): void }
class Dog extends Animal { constructor (name: string) { super(name) } sayHi (): void { console.log('汪汪汪') } }
const dog = new Dog('旺财') dog.sayHi()
|
类实现接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| interface Fruit { color: string get taste(): string }
interface Banana { price: number }
/** 如果使用继承extends,则无法同时继承多个类 */
class Apple implements Fruit, Banana { color: string = 'red' price: number = 100 get taste() { return '' } }
const apple = new Apple() console.log(apple)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class myClass<T> { public value: T constructor (value: T) { this.value = value } processData (input: T):T { console.log(this.value) return input } }
const myClassA = new myClass<string>('一年级') console.log(myClassA.processData('二年级'))
const myClassB = new myClass<number>(1) console.log(myClassB.processData(2))
|
__END__