关于javascript:typescript笔记

2次阅读

共计 978 个字符,预计需要花费 3 分钟才能阅读完成。

一、编译选项

  • 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
正文完
 0