建立一个数组很简单:ios
const arr = [1];
复制代码
此时 TypeScript 将会推断 arr
类型为 number[]
:git
arr.push('1'); // Error
复制代码
当数组元素具备其它类型时,能够经过类型注解的方式:github
const arr: Array<string | number> = [1];
arr.push('1'); // OK
arr.push(true); // Error
复制代码
或者你也能够经过可选元组的方式:typescript
const arr: [number, string?] = [1]; // arr 的成员类型能够是: number, string, undefined
arr.push('1'); // OK
arr.push(true); // Error
复制代码
使用元组形式,还能提供指定位置的类型检查:axios
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 里,描述了相同的问题。ui
如今,你能够经过断言的方式,来让程序正常运做:spa
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 })
。