安装与使用
- 安装
npm install -D typescript
- 使用
npx tsc -p apps/nav/ --watch
语法备忘
/* 标量类型 */
let name: string = "Henry";
let age: number = 25;
let sex: boolean = false;
let occupation: string;
occupation = "developer"
/* 数组类型 */
const arrayOfNumbers: number[] = []
arrayOfNumbers.push(12)
const arrayOfNumberOrString: (number|string)[] = []
arrayOfNumberOrString.push(12)
arrayOfNumberOrString.push("hello")
/* 对象类型 */
const person: {
name: string,
age: number,
sex: boolean,
// occupation: string | undefined
occupation?: string
} = {
name: "Henry",
age: 25,
sex: false
}
person.sex = true
person.occupation = "farmer"
/* 接口定义与限制选项 */
enum GENDER {
FEMALE,
MALE,
NONBINARY
}
interface Person {
name: string,
age: number,
sex: GENDER,
occupation?: string
}
const person:Person = {
name: "Henry",
age: 25,
sex: GENDER.MALE
}
/* 函数类型 */
const addNumber = (num: number): void => {...}