TypeScript支持与JavaScript几乎相同的数据类型,此外还提供了实用的枚举类型方便我们使用。
基础类型
boolean、number、string、null、undefined
let isDone: boolean = false;
let age: number = 13;
let binaryNumber: number = 0b1111;
let firstName: string = "haohao";
let message: string = `Hello,${firstName},age is ${age}`;
let u: undefined = undefined;
let n: null = null;
let num: number = undefined;
any类型
有时候变量的值可能是动态的,不知道它是什么类型的值时,想让它直接通过编译阶段的检查,这时候就用到了any
let notSure: any = "4";
notSure = `maybe it is a string`;
notSure = true;
notSure.myName;
notSure.getName();
firstName.charCodeAt;
notSure.charCodeAt;
void
void类型与any类型相反,它表示没有任何类型。 当一个函数没有返回值时,其返回值类型也是 void
function User(): void {
console.log("This is my warning message");
}
User();
let unusable: void = undefined;
never
never类型表示的是那些永不存在的值的类型。
function error(message: string): never {
throw new Error(message);
}
function fail() {
return error("Something failed");
}
function infiniteLoop(): never {
while (true) {
}
}
通俗理解 void 与 never: void 可以理解为一个没有 return 但能执行完毕的函数; never 是一个函数体无法结束执行的函数;
联合类型
let numberOrString: number | string = 5;
numberOrString = "55";
数组
let arrOfNumber: number[] = [1, 2, 3, 4];
arrOfNumber.push(5);
类数组
function test() {
console.log(arguments);
}
元祖
let user: [string, number] = ["string", 1];
类型断言
有时候你清楚地知道一个实体具有比它现有类型更确切的类型。 TypeScript会假设你,已经进行了必须的检查。
类型断言有两种形式。 其一是“尖括号”语法:
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
另一个为as语法:
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
类型别名
type PlusType = (x: number, y: number) => number;
function sum(x: number, y: number) {
return x + y;
}
const sum2: PlusType = sum;
object
这里是引用object表示非原始类型,也就是除number,string,boolean,symbol,null或undefined之外的类型。使用object类型,就可以更好的表示像Object.create这样的API。例如:
declare function create(o: object | null): void;
create({ prop: 0 });
create(null);
create(42);
create("string");
create(false);
create(undefined);
枚举
枚举类型可以由枚举值得到它的名字,这种感觉像对象的键值对。
const enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
const value = "UP";
if (value === Direction.Up) {
console.log("go up!");
}
interface接口
interface Person {
name: string;
age: number;
}
let viking: Person = {
name: "songJing",
age: 20,
};
interface PersonLiu {
name: string;
age?: number;
}
let vikingLiu: PersonLiu = {
name: "songJing",
};
interface PersonShe {
readonly id: number;
name: string;
age?: number;
}
let vikingShe: PersonShe = {
id: 1245,
name: "songJing",
};
泛型
function echo<T>(arg: T): T {
return arg;
}
function swap<T, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]];
}
const result2 = swap(["str", 12]);
function echoWithArr<T>(arg: T[]): T[] {
console.log(arg.length);
return arg;
}
const arrs = echoWithArr([1, 2, 3]);
interface IWithLength {
length: number;
}
function echoWithLength<T extends IWithLength>(arg: T): T {
console.log(arg.length);
return arg;
}
const str = echoWithLength("str");
const obj = echoWithLength({ length: 10, width: 10 });
const arr2 = echoWithLength([1, 2, 3]);
interface KeyPair<T, U> {
key: T;
value: U;
}
let kp1: KeyPair<number, string> = { key: 123, value: "str" };
let kp2: KeyPair<string, number> = { key: "22", value: 123 };
let arr: number[] = [1, 2, 3];
let arrTwo: Array<number> = [1, 2, 3];
interface IPlus<T> {
(a: T, b: T): T;
}
function plus(a: number, b: number): number {
return a + b;
}
function connect(a: string, b: string): string {
return a + b;
}
const a: IPlus<number> = plus;
const b: IPlus<string> = connect;
|