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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
let isDone: boolean = false let flag = true let str: string = 'bob' str = `test ${isDone}` let age: number = 40 let list: number[] = [1, 2, 3]
let x: [string, number] x = ['hi', 20]
console.log(x[0].substr(1)) enum Color { Red, Green, Blue } let c: Color = Color.Green let getCity getCity = function () { console.log(getCity); }
function test({ test = '我是测试' } = {}) { console.log(test) return test }
interface sTest { label: string width?: number readonly read?: boolean [propName: string]: any } function printLabel(labelObj: sTest) { console.log(labelObj.label); } printLabel({ label: 'sad', read: true, test: 'asd', ete: false })
let stest: sTest = { label: 'label', read: false }
stest.label = 'test'
let a: ReadonlyArray<number> = [1, 2, 3]
interface Sfun { (url: string, name?: boolean): boolean } let myFun: Sfun
myFun = function (ul, sds = false) { return ul && sds } myFun('123', Boolean(12)) myFun('123')
let myF myF = function (name: string, age?: number): Date { return new Date() } myFun('abc')
interface StringArr { [index: number]: string } let myArr: StringArr myArr = ['ABC', 'cde'] let myStr: string = myArr[0]
interface NumberDict { readonly [index: string]: number length: number } let myObj: NumberDict = { index: 12, length: 13 } myObj.length = 15
interface ClockInter { endTime: Date getTime(): Date } class Clock implements ClockInter { endTime: Date constructor(start: Date, step: number) { this.endTime = start } getTime() { return this.endTime } }
interface An { name: string } interface Cat extends An { color: string } interface 变异 { height: number } interface 老虎 extends Cat, 变异 { 称号: string } let blackCat: 老虎 = { name: '', color: 'red', height: 50, 称号: '森林之猫' }
interface Counter { (start: number): string interval: number reset(): void } function getCounter(): Counter { let counter = <Counter>function (start: number) { }; counter.interval = 123 counter.reset = function () { } return counter }
class Greeter { greeting: string constructor(message: string){ this.greeting = message } greet (): string{ return `HI ${this.greeting}` } } let greeter = new Greeter('Z').greet()
class 动物 { private 目: string constructor(name = '食肉目') { this.目 = name } 移动(距离: number = 0){ console.log(`${this.目}动物移动 ${距离} 米`) } 叫声(特征: string = 's'){ console.log(`动物叫声 ${特征}`) } } class 狗 extends 动物{ constructor(name = '食肉目 犬科 狗'){ super(name) } 移动(距离 = 10){ console.log('狗开始移动') super.移动(距离) } 叫声(){ super.叫声('汪汪') } } class 猫 extends 动物 { 移动(距离 = 15){ console.log('猫开始移动') super.移动(距离) } private 介绍(){ } } class 植物 { private 目: string constructor(name = '管状花目 旋花科 红薯') { this.目 = name } } let cuteDog = new 狗 let cuteCat = new 猫 let cuteAnimal = new 动物('可爱的动物') let eatPlant = new 植物
cuteAnimal = cuteDog cuteAnimal = cuteCat
|