巧用 TypeScript(三)

5,468 阅读3分钟

数组与元组

创建一个数组很简单:

const arr = [1];

此时 TypeScript 将会推断 arr 类型为 number[]

arr.push('1');  // Error

当数组元素具有其它类型时,可以通过类型注解的方式:

const arr: Array<string | number> = [1];

arr.push('1');  // OK
arr.push(true);  // Error

或者你也可以通过可选元组的方式:

const arr: [number, string?] = [1];         // arr 的成员类型可以是: number, string, undefined 
arr.push('1');   // OK
arr.push(true);   // Error

使用元组形式,还能提供指定位置的类型检查:

arr[0] = '1';   // Error
arr[1] = 1;    // Error

使用

通常,我们使用 Promise.all 并行发出多个请求:

interface A {
  name: string;
}

interface B {
  age: number;
}

const [{ data: a }, { data: b }] = await Promise.all([
  axios.get<A>('http://some.1'),
  axios.get<B>('http://some.2')
])

此时,TypeScript 能推出 a 的类型是 A, b 的类型是 B

现在,稍作改变:当满足特定条件时,才发出第二个请求:

// 使用类型注解
const requestList: [Promise<AxiosResponse<A>>, Promise<AxiosResponse<B>>?]
                    = [axios.get<A>('http://some.1')];
if (flag) {
  requestList[1] = (axios.get<B>('http://some.2'));
};
const [ { data: a }, response ] = await Promise.all(requestList);

我们期望它会如预想时那样工作,可是事与愿违,Promise.all(requestList),会出现类型兼容性的报错,在这个 Issues 里,描述了相同的问题。

现在,你可以通过断言的方式,来让程序正常运作:

const requestList: any[]  = [axios.get<A>('http://some.1')];  // 设置为 any[] 类型
if (flag) {
  requestList[1] = (axios.get<B>('http://some.2'));
}
const [
  { data: a },
  response
] = await Promise.all(requestList) as [AxiosResponse<A>, AxiosResponse<B>?] // 类型安全

字面量类型

在 JavaScript 基础上,TypeScript 扩展了一系列字面量类型,用来确保类型的准确性。

如创建一个字符串字面量:

const a = 'hello';  // a 的类型是 'hello'
a = 'world';   // Error

或者你也可以:

let a: 'hello' = 'hello';  // a 的类型是 'hello'
a = 'world';     // Error

其它数据类型与此相似。

你也可以定义交叉类型与联合类型的字面量:

interface A {
  name: string;
}
interface B {
  name: string;
  age: number;
}

type C = A | B;
type D = A & B;

对象字面量类型

对于对象字面量的类型,TypeScript 有一个被称之为 「Freshness 」的概念,它也被称为更严格的对象字面量检查,如下例子:

let someThing: { name: string };
someThing = { name: 'hello' };              // ok
someThing = { name: 'hello', age: 123 };    // Error, 对象字面量只能指定已知属性, { name: string } 类型中不存在 age 属性

let otherThing = { name: 'hello', age: 123 };
someThing = otherThing;                     // ok

TypeScript 认为创建的每个对象字面量都是 「 fresh 」 状态;当一个 「 fresh 」 对象字面量赋值给一个变量时,如果对象的类型与变量类型不兼容时,会出现报错(如上例子中 someThine = { name: 'hello', age: 123 }; 的错误);当对象字面量的类型变宽,对象字面量的 「 fresh 」 状态会消失(如上例子中 someThing = otherThing; ,赋值以后,someThing 的类型变宽)。

一个更实际的用例如下:

function logName(something: { name: string }) {
  console.log(something.name);
}

const obj = {
  name: 'matt',
  job: 'being awesome'
}

logName(obj); // ok
logName({ name: 'matt' }); // ok
logName({ nama: 'matt' }); // Error: nama 属性在 { name: string } 属性中不存在。
logName({ name: 'matt', job: 'being awesome' }); // Error: 对象字面量只能指定已知属性,`job` 属性在这里并不存在。

基本原理与上文中相似,当想用更严格的类型检查时,可以传一个具有 fresh 状态的对象字面量(如 logName({ name: 'matt', job: 'being awesome' });)。当你想多传一些属性至函数,可以将对象字面量赋值至一个新变量,然后再传至函数(如 logName(obj))。或者你也可以通过给函数形参添加多余类型的方式 function logName(someThing: { name: string; [key: string]: string })

更多