一、编译选项
- outDir: 编译输入目录
- target: 指定编译的代码版本指标 默认ES3
- watch: 监听模式,只有文件变更,会主动编译
// 命令行模式
tsc --outDir ./dist --target es6 ./src/helloTypescript.ts
tsc -p ./config/xxx.json // -p能够指定配置文件目录
// 配置文件形式 tsconfig.json
{
compilerOptions:{
target:'es5',
outDir:'./dist',
watch:true,
strictNullCheck:true // 会检测null
},
include:["./src/**/*"] // 寻找src下的所有子目录的所有文件
}
二、类型零碎
1、类型标注
// 根底类型
let title: string = "题目";
let n: number = 100;
let isOk: boolean = true;
// 空和未定义类型 Null和undefined
let un: undefined;
un = 1; // error 只能是undefined
let o: null;
o = 1; // error 只能是null
// null和undefined是所有类型的子类型
let a: string = '哈哈';
a = null; // 能够
a = undefined; // 能够
// 未赋值状况下,值为undefined
// 同时未声明类型 则是any类型
let b;
// strictNullCheck 会检测null ele是any或者null
let ele = document.querySelector('#box');
const id = ele.id // error ele可能是null
let obj: object = {
x: 1,
y: 2
}
// object只是通知obj是一个对象 有.toString等办法,然而没有阐明有x属性
obj.x = 3; // error
let obj2: { x: number, y: number } = {
x: 1,
y: 2
}
obj2.x = 5;
// 内置对象类型
let d1: Date = new Date();
let st = new Set([1, 2]);
// 包装器类型
// 包装器类型不能赋值给根底类型
let str1 = new String('abc');
str1 = 'def'; // ok
let str2 = 'abc';
str2 = new String('def'); // error
发表回复