let isDone: boolean = false
let age: number = 22
let myName: string = 'lq'
let sentence: string = `My name is ${myName}
I'll be ${ age + 1 } next year
`
let ageList: number[] = [1, 2, 3]
let ageList1: Array<number> = [1, 2, 3]
// 元組 tuple 元組型別允許表示一個已知元素數量和型別的陣列,各元素的型別不必相同
let tup: [string, number]
tup = ['hello', 22]
// 列舉 enum型別,列舉型別可以為一組數值賦予友好的名字
enum Color {
Red,
Green,
Blue
}
let c: Color = Color.Green
enum Red {
DarkRed = 1,
LightRed = 4
}
let redName: string = Red[4]
function warn (): void {
console.log('nothing')
}
function print (person: string) {
return 'Hello ' + person
}
// 介面
interface Person {
firstName: string
lastName: string
}
const user = {
firstName: 'L',
lastName: 'Q'
}
function hello (person: Person) {
return 'Hello ' + person.firstName
}
const result = hello(user)
class Student {
public fullName: string
constructor (public firstName, public lastName) {
this.fullName = firstName + lastName
}
}
const xiaoming = new Student('xiao', 'ming')
function greet (person: Person) {
return 'Hello ' + person.firstName + person.lastName
}
greet(xiaoming)
1. 型別註解: 在初始化變數定義變數型別,函式形參型別,函式返回值型別
2. 介面
介面也是一種型別,可以將變數,引數,返回值這些資料定義成這種型別,表示該值包含了介面要求的結構