Skip to content

数据类型

布尔类型(boolean)

typescript
let married: boolean=false;

数字类型(number

typescript
let age: number=10;

字符串类型(string)

typescript
let firstname: string='zs';

数组类型(array)

typescript
let arr2: number[]=[4,5,6];
let arr3: Array<number>=[7,8,9];

元组类型(tuple)

  • 在 TypeScript 的基础类型中,元组( Tuple )表示一个已知数量和类型的数组
typescript
let zs:[string,number] = ['zs',5];
zhufeng[0].length;
zhufeng[1].toFixed(2);
元组数组
每一项可以是不同的类型每一项都是同一种类型
有预定义的长度没有长度限制
用于表示一个固定的结构用于表示一个列表
typescript
const animal:[string,number,boolean] = ['zs',10,true];

枚举类型(enum)

  • 事先考虑某一个变量的所有的可能的值,尽量用自然语言中的单词表示它的每一个值
  • 比如性别、月份、星期、颜色、单位、学历

普通枚举

typescript
enum Gender{
    GIRL,
    BOY
}
console.log(Gender.GIRL); // 0
console.log(Gender.BOY); // 1
console.log(Gender[0]); // GIRL
console.log(Gender[1]); // BOY

enum Week{
    MONDAY=1,
    TUESDAY=2
}
console.log(Week.MONDAY);  // 1

常数枚举

  • 常数枚举与普通枚举的区别是,它会在编译阶段被删除,并且不能包含计算成员
  • 假如包含了计算成员,则会在编译阶段报错
typescript
const enum Colors {
    Red,
    Yellow,
    Blue
}

console.log([Colors.Red, Colors.Yellow, Colors.Blue]); // [ 0, 1, 2 ]
// 编译完,由于常量不会变,所以不需要计算了
console.log([0 /* Red */, 1 /* Yellow */, 2 /* Blue */]); // [ 0, 1, 2 ]

任意类型(any)

  • any就是可以赋值给任意类型
  • 第三方库没有提供类型文件时可以使用any
  • 类型转换遇到困难时
  • 数据结构太复杂难以定义
typescript
let root:any=document.getElementById('root');
root.style.color='red';

let root:(HTMLElement|null)=document.getElementById('root');
root!.style.color='red';//非空断言操作符

null 和 undefined

  • null 和 undefined 是其它类型的子类型,可以赋值给其它类型,如数字类型,此时,赋值后的类型会变成 null 或 undefined
  • strictNullChecks 参数用于新的严格空检查模式,在严格空检查模式下, null 和 undefined 值都不属于任何一个类型,它们只能赋值给自己这种类型或者 any
typescript
let x: number;
x = 1;
x = undefined;    
x = null;   

let y: number | null | undefined;
y = 1;
y = undefined;   
y = null;

ts.config.json

json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
+    "strictNullChecks": true,
  }
}

严格模式下

typescript
let x: undefined = undefined
let y: null = null
let z: any = undefined

void 类型

  • void 表示没有任何类型
  • 当一个函数没有返回值时,TS 会认为它的返回值是 void 类型。
typescript
function greeting(name:string):void {
    console.log('hello',name);
    //当我们声明一个变量类型是 void 的时候,它的非严格模式(strictNullChecks:false)下仅可以被赋值为 null 和 undefined
    //严格模式(strictNullChecks:true)下只能返回undefined
    //return null;
    //return undefined;
}

never类型

never是其它类型(null undefined)的子类型,代表不会出现的值

作为不会返回( return )的函数的返回值类型

typescript
// 返回never的函数 必须存在 无法达到( unreachable ) 的终点
function error(message: string): never {
    throw new Error(message); // 直接以后结束了
}
let result1 = error('hello');
// 由类型推论得到返回值为 never
function fail() {
    return error("Something failed");
}
let result = fail();

// 返回never的函数 必须存在 无法达到( unreachable ) 的终点
function infiniteLoop(): never {
    while (true) {}
}

strictNullChecks

  • 在 TS 中, null 和 undefined 是任何类型的有效值,所以无法正确地检测它们是否被错误地使用。于是 TS 引入了 --strictNullChecks 这一种检查模式
  • 由于引入了 --strictNullChecks ,在这一模式下,null 和 undefined 能被检测到。所以 TS 需要一种新的底部类型( bottom type )。所以就引入了 never。
typescript
// Compiled with --strictNullChecks
function fn(x: number | string) {
  if (typeof x === 'number') {
    // x: number 类型
  } else if (typeof x === 'string') {
    // x: string 类型
  } else {
    // x: never 类型
    // --strictNullChecks 模式下,这里的代码将不会被执行,x 无法被观察
  }
}

never 和 void 的区别

  • void 可以被赋值为 null 和 undefined的类型。 never 则是一个不包含值的类型。
  • 拥有 void 返回值类型的函数能正常运行。拥有 never 返回值类型的函数无法正常返回,无法终止,或会抛出异常。

Symbol

  • 我们在使用 Symbol 的时候,必须添加 es6 的编译辅助库
  • Symbol 是在ES2015之后成为新的原始类型,它通过 Symbol 构造函数创建
  • Symbol 的值是唯一不变的
typescript
const sym1 = Symbol('key');
const sym2 = Symbol('key');
console.log(Symbol('key') === Symbol('key')); // false

ts.config.json

json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strictNullChecks": true,
+    "lib": ["ESNext", "DOM"]
  }
}

BigInt

  • 使用 BigInt 可以安全地存储和操作大整数
  • 我们在使用BigInt的时候,必须添加ESNext的编译辅助库
  • 要使用1n需要"target": "ESNext"
  • number和 BigInt类型不一样,不兼容
typescript
const max = Number.MAX_SAFE_INTEGER;// 2**53-1
console.log(max + 1 === max + 2); // true
typescript
const max = BigInt(Number.MAX_SAFE_INTEGER);
console.log(max + 1n === max + 2n); // false 修改ts配置, "target": "ESNEXT",
console.log(max + BigInt(1) === max + BigInt(2)); // false

ts.config.json

json
{
  "compilerOptions": {
-    "target": "es5",
+    "target": "ESNEXT",
    "module": "commonjs",
    "strictNullChecks": true,
    "lib": ["ESNext", "DOM"]
  }
}

number类型和bigint类型不兼容

Number 和 BigInt 是js类型 number 和 biginit 是ts类型

typescript
let foo: number;
let bar: bigint;
foo =bar; // Type 'bigint' is not assignable to type 'number'.t
bar = foo; // Type 'number' is not assignable to type 'bigint'.

类型推论

  • 是指编程语言中能够自动推导出值的类型的能力,它是一些强静态类型语言中出现的特性
  • 定义时未赋值就会推论成any类型
  • 如果定义的时候就赋值就能利用到类型推论
typescript
let username2;
username2 = 10;
username2 = 'zs';
username2 = null;

包装对象(Wrapper Object)

  • JavaScript 的类型分为两种:原始数据类型(Primitive data types)和对象类型(Object types)。
  • 所有的原始数据类型都没有属性(property)
  • 原始数据类型
    • 布尔值
    • 数值
    • 字符串
    • null
    • undefined
    • Symbol
javascript
let name = 'zs';
console.log(name.toUpperCase());

console.log((new String('zs')).toUpperCase());
  • 当调用基本数据类型方法的时候,JavaScript 会在原始数据类型和对象类型之间做一个迅速的强制性切换
typescript
let isOK: boolean = true; // 编译通过
let isOK: boolean = Boolean(1) // 编译通过
let isOK: boolean = new Boolean(1); // 编译失败   期望的 isOK 是一个原始数据类型

联合类型

  • 联合类型(Union Types)表示取值可以为多种类型中的一种
  • 未赋值时联合类型上只能访问两个类型共有的属性和方法
typescript
let name: string | number;
console.log(name!.toString());
name = 3;
console.log(name.toFixed(2));
name = 'zs';
console.log(name.length);

export {};
## 类型断言
  • 类型断言可以将一个联合类型的变量,指定为一个更加具体的类型
  • 不能将联合类型断言为不存在的类型
typescript
let name: string | number;
console.log((name! as string).length);
console.log((name! as number).toFixed(2));
console.log((name! as boolean)); // onversion of type 'string | number' to type 'boolean' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
export {};

双重断言

typescript
interface Person {
    name: string;
    age: number;
}
const person = 'zs' as any as Person; // ok

字面量类型和类型字面量

  • 字面量类型的要和实际的值的字面量一一对应,如果不一致就会报错
  • 类型字面量和对象字面量的语法很相似
typescript
const up:'Up'= 'Up';
const down: "Down" = "Down";
const left: "Left" = "Left";
const right: "Right" = "Right";
type Direction = 'Up' | 'Down' | 'Left' | 'Right';
// 可以实现类似枚举的效果
function move(direction: Direction) {}
move("Up");
typescript
type Person = {
  name:string,
  age:number
};

let p1: Person = {
  name: 'zhangsan',
  age: 10
}

字符串字面量 vs 联合类型

  • 字符串字面量类型用来约束取值只能是某几个字符串中的一个, 联合类型(Union Types)表示取值可以为多种类型中的一种
  • 字符串字面量 限定了使用该字面量的地方仅接受特定的值,联合类型 对于值并没有限定,仅仅限定值的类型需要保持一致
typescript
type T1 = '1' | '2' | '3'
type T2 = string | number | boolean
let t1:T1 = '1'
let t2:T2 = true