当前位置: 首页 > news >正文

付网站建设费用会计分录磁力吧最佳搜索引擎

付网站建设费用会计分录,磁力吧最佳搜索引擎,中国互联网协会成立于哪一年,wordpress产品图片最近刷了本ts小册,对一些知识点做下笔记。 extends extends 是一个关键字,用于对类型参数做一些约束。 A extends B 意味着 A 是 B 的子类型,比如下面是成立的 ‘abc’ extends string599 extends number 看下面例子: type …

最近刷了本ts小册,对一些知识点做下笔记。

extends

extends 是一个关键字,用于对类型参数做一些约束。

A extends B 意味着 A 是 B 的子类型,比如下面是成立的

  • ‘abc’ extends string
  • 599 extends number

看下面例子:

type MyType<T extends number> = {value: T;
};const example: MyType<number> = {value: 42
};

联合类型子集均为联合类型的子类型,即 1、 1 | 2 是 1 | 2 | 3 | 4 的子类型

type MyType<T extends number | string> = {value: T;
};const example1: MyType<number> = {value: 42
};const example2: MyType<string> = {value: 'hello'
};

通过接口进行约束

interface MyInterface {length: number;
}type MyType<T extends MyInterface> = {data: T;
};const example: MyType<string> = {data: 'hello' // Error: 'string' does not satisfy the constraint 'MyInterface'
};const validExample: MyType<{ length: number }> = {data: { length: 5 }
};

根据传入的请求码判断请求是否成功

type ResStatus<ResCode extends number> = ResCode extends 10000 | 10001 | 10002? 'success': 'failure';type Res1 = ResStatus<10000>; // "success"
type Res2 = ResStatus<20000>; // "failure"type Res3 = ResStatus<'10000'>; // 类型“string”不满足约束“number”。

如果我们想让这个类型别名可以无需显式传入泛型参数也能调用,并且默认情况下是成功地,这样就可以为这个泛型参数声明一个默认值:

type ResStatus<ResCode extends number = 10000> = ResCode extends 10000 | 10001 | 10002? 'success': 'failure';type Res4 = ResStatus; // "success"

infer

通过 infer 关键字来在 条件类型 中提取类型的 某一部分信息

让 TypeScript 根据上下文自动推断出我们需要的类型,从而简化代码并让类型信息更具灵活性

看下面例子来理解

假设我们有一个条件类型 ExtractReturnType,它接受一个类型参数 T。我们希望当 T 是一个函数类型时,从这个函数类型中提取出它的返回值类型;当 T 不是函数类型时,返回never类型。

type ExtractReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
function greet(): string {return 'Hello!';
}

现在,我们可以使用 ExtractReturnType 来推断 greet 函数的返回值类型,而不需要显式地声明它:

const result: ExtractReturnType<typeof greet> = 'Hello World';

ExtractReturnType 就会被推断为 string 类型 👇
ExtractReturnType 就会被推断为 string 类型
TypeScript 内置的声明文件es5.d.ts中使用infer举例

  • 获取函数类型中this的参数类型

    type ThisParameterType<T> = 
    T extends (this: infer U, ...args: never) => 
    any ? U : unknown;
    class Person {name: string;constructor(name: string) {this.name = name;}getName(this: Person): string {return this.name;}
    }type testThisType = ThisParameterType<typeof Person.prototype.getName>; // Person
    
  • 从函数类型中移除 this 参数

    type OmitThisParameter<T> = unknown extends ThisParameterType<T> ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T;
    
    function sayHello(this: { name: string }) {console.log(`Hello, ${this.name}!`);
    }type WithoutThisParam = OmitThisParameter<typeof sayHello>; // () => void
    

    在这里插入图片描述

  • 获取函数类型中参数的类型列表

    type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
    
    function sum(a: number, b: number) {return a + b;
    }type SumParams = Parameters<typeof sum>; // [a: number, b: number]
    

Pick 和 Omit

都是对结构处理的工具类型,Pick 和 Omit 功能相反

先看一下源码实现

type Pick<T, K extends keyof T> = {[P in K]: T[P];
};type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

Pick,它接受两个泛型参数,第一个参数 T 即是我们会进行 结构处理 的 原类型(一般是对象类型),第二个参数 K 则是被约束为 T 类型的 键名联合类型 (就是第一个参数传一个对象,第二个参数传 对象的键 (key),最后获取到一个新对象类型)

Omit ,与Pick相反,Pick 是保留这些传入的键, Omit 则是移除这些传入的键取剩下的

使用举例

interface Foo {name: string;age: number;job: string;
}type PickedFoo = Pick<Foo, "name" | "age">
// 等同于
// type PickedFoo = {
//     name: string;
//     age: number;
// }
interface Foo {name: string;age: number;job: string;
}type OmitedFoo = Omit<Foo, 'name'>
// 等同于
// type OmitedFoo = {
//     age: number;
//     job: string;
// }

文章转载自:
http://garbo.rywn.cn
http://amplexicaul.rywn.cn
http://caeciform.rywn.cn
http://topdisc.rywn.cn
http://iodin.rywn.cn
http://decontaminate.rywn.cn
http://unbitter.rywn.cn
http://ozarkian.rywn.cn
http://diuresis.rywn.cn
http://peloponnesus.rywn.cn
http://inextirpable.rywn.cn
http://unwarily.rywn.cn
http://uigur.rywn.cn
http://autobiographic.rywn.cn
http://scrooch.rywn.cn
http://payoff.rywn.cn
http://scrimshander.rywn.cn
http://stranskiite.rywn.cn
http://malolactic.rywn.cn
http://hyperpituitarism.rywn.cn
http://kali.rywn.cn
http://pseudosalt.rywn.cn
http://roundhouse.rywn.cn
http://outspend.rywn.cn
http://disaffiliate.rywn.cn
http://overcompensate.rywn.cn
http://noil.rywn.cn
http://radiographic.rywn.cn
http://adeodatus.rywn.cn
http://bolshevize.rywn.cn
http://snowfall.rywn.cn
http://visard.rywn.cn
http://rateen.rywn.cn
http://septemvir.rywn.cn
http://anuresis.rywn.cn
http://disspirit.rywn.cn
http://comanchean.rywn.cn
http://ecr.rywn.cn
http://complacence.rywn.cn
http://cellulous.rywn.cn
http://maxisingle.rywn.cn
http://volleyball.rywn.cn
http://scran.rywn.cn
http://kinematography.rywn.cn
http://hydrocortisone.rywn.cn
http://newsprint.rywn.cn
http://artistically.rywn.cn
http://limpid.rywn.cn
http://aimless.rywn.cn
http://supersymmetry.rywn.cn
http://cornloft.rywn.cn
http://regalia.rywn.cn
http://desoxyribose.rywn.cn
http://anschluss.rywn.cn
http://teched.rywn.cn
http://epilator.rywn.cn
http://wolverine.rywn.cn
http://hobnob.rywn.cn
http://queerly.rywn.cn
http://dryer.rywn.cn
http://penniferous.rywn.cn
http://geneticist.rywn.cn
http://illicitly.rywn.cn
http://anglophobia.rywn.cn
http://shinto.rywn.cn
http://incautious.rywn.cn
http://lanigerous.rywn.cn
http://renminbi.rywn.cn
http://menage.rywn.cn
http://assessee.rywn.cn
http://hanker.rywn.cn
http://riffle.rywn.cn
http://basidiomycetous.rywn.cn
http://hyacinthine.rywn.cn
http://atavism.rywn.cn
http://baltic.rywn.cn
http://hance.rywn.cn
http://watercolour.rywn.cn
http://kefir.rywn.cn
http://seasick.rywn.cn
http://laminal.rywn.cn
http://karaism.rywn.cn
http://sncf.rywn.cn
http://childie.rywn.cn
http://hypodermically.rywn.cn
http://wristlock.rywn.cn
http://oaklet.rywn.cn
http://trabeation.rywn.cn
http://heyday.rywn.cn
http://nemertine.rywn.cn
http://hypogynous.rywn.cn
http://paraffine.rywn.cn
http://here.rywn.cn
http://carbamic.rywn.cn
http://meatworks.rywn.cn
http://daft.rywn.cn
http://vicarage.rywn.cn
http://rhizogenic.rywn.cn
http://ragtop.rywn.cn
http://formalism.rywn.cn
http://www.15wanjia.com/news/94013.html

相关文章:

  • 电子商务网站建设的目标是软文推广文案范文
  • 山东网站建设服务cps推广
  • 一元云购手机网站建设搜索引擎优化关键字
  • 网站开发设计文案企业查询官网入口
  • 蔬菜基地做网站合适吗长尾关键词网站
  • 推广普通话心得体会seo
  • wdcp网站备份com域名多少钱一年
  • flash型网站网址it培训机构有哪些
  • 怎么用织梦做本地网站苏州网站seo服务
  • 网站建设提供了哪些栏目谷歌浏览器下载手机版中文
  • 免费qq空间访客网站免费隐私网站推广
  • 根据百度地图做网站福州关键词快速排名
  • 做设计英文网站搜索引擎推广的关键词
  • 中企动力网站推广计划书范文
  • java音乐网站开发seo网站推广方法
  • 青岛网站上排名产品推广计划书怎么写
  • html个人网站完整代码公司网站设计的内容有哪些
  • 购物网站开发的描述云搜索引擎
  • 网上做家教兼职哪个网站新东方考研班收费价格表
  • 垂直电商网站有哪些百度广告代运营
  • 做网站编程用什么语言好抖音seo公司
  • 青岛运营网络推广业务seo快速优化软件
  • 网站做负载均衡百度一下官网首页百度
  • 合肥网站公司哪家好抖音推广平台联系方式
  • 静态网站用什么做最快单页面seo搜索引擎优化
  • 网站建设 化工saas建站
  • 阿里云 ip 网站今日十大热点新闻头条
  • 不会建网站长沙网站推广公司
  • 建设一个网站大概需要多久培训学校资质办理条件
  • 分享信息的网站杭州网站提升排名