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
| let id: number = 5 let company: string = "Apple Inc." let isPublished: boolean = true let x: any = "hello"
let ids: number[] = [1, 2, 3, 4, 5] let arr: any[] = [1, true, "hello"]
let person: [number, boolean, string] = [1, true, "Charry"]
let employee: [number, string][]
employee = [ [1, "Tom"], [2, "Jerry"], ]
let productID: string | number productID = 22 productID = "iPhone📱"
enum directions { Up, Down, Left, Right, }
type User = { id: number, name: string }
const user: User = { id: 1, name: "John" }
let cid: any = 1
let customerID = cid as number customerID = 2
function addNum(x: number, y: number): number { return x + y }
function log(message: string | number): void { console.log(message) }
interface userInterface { readonly id: number, name: string, age: number }
const newUser: userInterface = { id: 1, name: "John", age: 18 }
interface mathFunc { (x: number, y: number): number }
const add: mathFunc = (x: number, y: number) => x + y const sub: mathFunc = (x: number, y: number) => x - y
interface PersonInterface { id: number name: string register(): string }
class Person implements PersonInterface { id: number name: string
constructor(id: number, name: string) { this.id = id this.name = name }
register() { return `${this.name} is registered now.` } }
class Employee extends Person { department: string
constructor(id: number, name: string, department: string) { super(id, name) this.department = department } }
const jackson = new Employee(1, "Jackson", "Dep of Fin") console.log(jackson)
function getArray<T>(items: T[]): T[] { return new Array().concat(items) }
let numArray = getArray<number>([1, 2, 3, 4]) let strArray = getArray<string>(["hello", "world"])
|