![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
typescript
ikun不解释
前端开发
展开
-
用自定义 hooks 管理请求状态
用自定义 hooks 管理请求状态创建 hooksinterface State<D> { data: D | null; error: Error | null; stat: 'loading' | 'success' | 'error' | 'idle';};const defaultState: State<null> = { data: null, // 数据 error: null, // 错误信息 stat: 'idle', // 状态}原创 2022-01-09 11:14:55 · 474 阅读 · 0 评论 -
封装fetch请求
封装import qs from 'qs'interface Config extends RequestInit { data?: object; token?: string;}const API_PREFIX = process.env.API_PREFIXexport const http = (endPoint: string, {data, token, ...customConfig}: Config = {}) => { // 默认参数 const conf.原创 2021-12-19 16:03:12 · 484 阅读 · 0 评论 -
typescript(5):枚举
枚举数字枚举// up使用的初始为1,其余的成员自动增长enum direction { up = 1, down, left, right}// 不使用初始化器,自增长,初始值为0// 枚举成员的值都是不同的字符串的枚举在字符串枚举里,每个成员都必须用字符串字面量,或另一个字符串枚举成员进行初始化enum direction { up="up", down="down", left="left", right原创 2020-12-02 10:44:52 · 126 阅读 · 0 评论 -
typescript(4):泛型
泛型介绍可以使用泛型来创建可重用的组件,一个组件可以支持多种类型的数据// 返回值的类型与传入参数的类型相同function identity<T>(arg: T):T{ return arg}使用泛型变量使用泛型创建像 identity 这样的泛型时,编辑器要求在函数体必须正确的使用这个通用的类型。也就是说,必须把这个参数当做是任意或者所有类型// 不确定arg的类型,是不是有Length属性function identity<T>(a原创 2020-12-02 09:57:16 · 194 阅读 · 0 评论 -
typescript(3): 函数
函数函数类型为函数定义类型function add(x: number, y: number) { return x+y}let myAdd = function(x: number, y: number): number { return x+y}可以给每个参数添加类型之后再为函数本身添加返回值类型。Typescript 能够根据返回语句自动判断出返回值类型,因此通常省略它书写完整函数类型let myAdd: (x: number, y: numbe原创 2020-12-02 08:51:30 · 199 阅读 · 0 评论 -
typescript(3):类
类示例class Greeter { greeting: string constructor(message: string){ this.greeting = message } greet(){ return `hello, ${this.greeting}` }}let greeter = new Greeter("world")继承基于类的程序设计中一种最基本的模式是允许使用继承来扩展现有的类。原创 2020-12-01 17:41:57 · 162 阅读 · 0 评论 -
typescript(2):接口
接口介绍在 typescript 中,接口的作用就是为类型检查命名和为代码获第三方代码提供契约基本写法interface label { labelStr: string}可选属性接口里的属性不全都是必需的。带有可选属性的接口预普通的接口定义差不多,只是在可选属性名字定义的后面加上一个 ? 符号可选属性的书写方法interface config { color?: string, width?: string}只读属性只能在原创 2020-12-01 15:22:06 · 111 阅读 · 0 评论 -
typescript入门(一)
一、基本数据类型布尔值let isDone: boolean = false数字let num: number = 1字符串let name: string = 's'数组// 第一种:可以在元素类型后面接上[],表示由此类型元素组成的一个数组let list: number[] = [1,2,3]// 第二种方式是使用数组泛型let list: Array<number> = [1,2,3]元组(Tuple)元组类型允许表示一个已知元素数量原创 2020-10-26 16:39:43 · 82 阅读 · 2 评论