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
| const anExampleVariable = "Hello World 123" console.log(anExampleVariable)
let isDone: boolean = false; console.log(isDone)
let decLiteral: number = 2023; let binaryLiteral: number = 0b11111100111; let octalLiteral: number = 0o3747; let hexLiteral: number = 0x7e7;
let nickName: string = "Jacky";
let list: number[] = [1, 2, 3];
let list2: Array<number> = [1, 2, 3];
let x: [string, number]; x = ['hello', 10];
enum Color {Red, Green, Blue}; let c: Color = Color.Green;
let notSure: unknown = 4; notSure = 'maybe a string instead'; notSure = false;
function test(): void { console.log('This is function is void'); }
let u: undefined = undefined; let n: null = null;
let myFavoriteNumber: string | number; myFavoriteNumber = 'seven'; myFavoriteNumber = 7;
var num:number = 5 if (num > 0) { console.log('数字是正数') }
var grade:string = 'A'; switch(grade) { case 'A': { console.log('优'); break; } case 'B': { console.log('良'); break; } case 'C': { console.log('及格'); break; } case 'D': { console.log('不及格'); break; } default: { console.log('非法输入'); break; } }
function add(x: any, y: any) { return x + y; }
let myAdd = function (x: number, y: number) { return x + y; };
console.log(myAdd(1, 2))
function buildName(firstName: string, lastName?: string) { if (lastName) return firstName + ' ' + lastName; else return firstName; }
let result1 = buildName('Bob'); let result2 = buildName('Bob', 'Adams');
function getEmployeeName(firstName: string, ...restOfName: string[]) { return firstName + ' ' + restOfName.join(' '); }
let employeeName = getEmployeeName('Joseph', 'Samuel', 'Lucas', 'MacKinzie'); console.log(employeeName)
function testNumber(num: number) { if (num > 0) { console.log(num + ' 是正数'); } else if (num < 0) { console.log(num + ' 是负数'); } else { console.log(num + ' 为0'); } }
let testArrowFun = (num: number) => { if (num > 0) { console.log(num + ' 是正数'); } else if (num < 0) { console.log(num + ' 是负数'); } else { console.log(num + ' 为0'); } }
class Person { private name: string private age: number
constructor(name: string, age: number) { this.name = name; this.age = age; }
public getPersonInfo(): string { return `My name is ${this.name} and age is ${this.age}`; } } let person1 = new Person('Jacky', 18); person1.getPersonInfo();
class Employee extends Person { private department: string
constructor(name: string, age: number, department: string) { super(name, age); this.department = department; }
public getEmployeeInfo(): string { return this.getPersonInfo() + ` and work in ${this.department}`; } }
let someArray = [1, "string", false];
for (let entry of someArray) { console.log(entry); }
for (let i in someArray) { console.log(i); }
|