1. 类的实例成员
2. 类的方法
3. 函数参数
4. 函数的返回值

···

``````class Queue {
private data = [];
push = item => this.data.push(item);
pop = () => this.data.shift();
}
``````

···

···

``````class Queue {
private data = [];
push = item => this.data.push(item);
pop = () => this.data.shift();
}

const queue = new Queue();

queue.push(0);
queue.push('1'); // Oops，一个错误

// 一个使用者，走入了误区
console.log(queue.pop().toPrecision(1));
console.log(queue.pop().toPrecision(1)); // RUNTIME ERROR
``````

···

``````class QueueNumber{

}
``````

``````class QueueString{

}
``````

``````// 创建一个泛型类
class Queue<T> {
private data: T[] = [];
push = (item: T) => this.data.push(item);
pop = (): T | undefined => this.data.shift();
}

// 简单的使用
const queue = new Queue<number>();
queue.push(0);
queue.push('1'); // Error：不能推入一个 `string`，只有 number 类型被允许
``````

``````function reverse<T>(items: T[]): T[] {
const toreturn = [];
for (let i = items.length - 1; i >= 0; i--) {
toreturn.push(items[i]);
}
}

const sample = [1, 2, 3];
let reversed = reverse(sample);

reversed = '1'; // Error
reversed = ['1', '2']; // Error

reversed = 1; // ok
reversed = [1, 2]; // ok
``````
``````TIP

``````

``````declare function foo<T>(arg: T): void; // 不需要

declare function foo(arg: any): void; // 正确用法

``````

``````const getJSON = <T>(config: { url: string; headers?: { [key: string]: string } }): Promise<T> => {
const fetchConfig = {
method: 'GET',
Accept: 'application/json',
'Content-Type': 'application/json',
};
return fetch(config.url, fetchConfig).then<T>(response => response.json());
};
``````

``````// 请求接口数据
export interface ResponseData<T = any> {
/**
* 状态码
* @type { number }
*/
code: number;

/**
* 数据
* @type { T }
*/
result: T;

/**
* 消息
* @type { string }
*/
message: string;
}
``````

``````// 在 axios.ts 文件中对 axios 进行了处理，例如添加通用配置、拦截器等
import Ax from './axios';

import { ResponseData } from './interface.ts';

export function getUser<T>() {
return Ax.get<ResponseData<T>>('/somepath')
.then(res => res.data)
.catch(err => console.error(err));
}
``````

``````interface User {
name: string;
age: number;
}

async function test() {
// user 被推断出为
// {
//  code: number,
//  result: { name: string, age: number },
//  message: string
// }
const user = await getUser<User>();
}
``````
{"ft_lang_label":"__label__zh","ft_lang_prob":0.55823916,"math_prob":0.845337,"size":2753,"snap":"2020-34-2020-40","text_gpt3_token_len":1186,"char_repetition_ratio":0.10658421,"word_repetition_ratio":0.08152174,"special_character_ratio":0.33309117,"punctuation_ratio":0.25678498,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98886824,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-13T06:09:49Z\",\"WARC-Record-ID\":\"<urn:uuid:a16e6c94-39ad-4c1d-ab14-12e6303f561b>\",\"Content-Length\":\"26462\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:150ebda0-240d-47ed-b099-ef4a798b0aed>\",\"WARC-Concurrent-To\":\"<urn:uuid:939807ae-c8ea-44f7-a6ab-2db98e01d6d2>\",\"WARC-IP-Address\":\"104.27.191.234\",\"WARC-Target-URI\":\"https://houyewei.com/typescript-fan-xing/\",\"WARC-Payload-Digest\":\"sha1:IYTVYTFETX4UU5H5TP6UM7UWEOKXMTGU\",\"WARC-Block-Digest\":\"sha1:TAVV3ZGBVQBPCYOJBWVEJQ37ONNK2GWA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738960.69_warc_CC-MAIN-20200813043927-20200813073927-00358.warc.gz\"}"}