ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[TypeScript] infer

2022-07-19 16:32:30  阅读:191  来源: 互联网

标签:RT TypeScript any extends ReturnOf type infer


Res1: https://www.typescript-training.com/course/making-typescript-stick/08-type-challenges/#returnoff

Res2: https://learntypescript.dev/09/l2-conditional-infer

 

There is an infer keyword that can be used within a condition in a conditional type to put the inferred type into a variable. That inferred variable can then be used within the conditional branches.

 

type ArrayElementType<T> = T extends (infer E)[] ? E : T;

// type of item1 is number
type item1 = ArrayElementType<number[]>

// type of item2 is `{name: string}`
type item2 = ArrayElementType<{name: string}>

 

For item1, T is number[], so (infer E)[] would match number[], therefore E is number type

For item2, T is {name: string}, so (infer E)[] doesn't match (not array of any type), therefore E would be T which is {name: string}

 

Infer function return type example:

type ReturnOf<F> = F extends (...args: any[]) => infer RT ? RT: F
// F extends (...args: any[]) => any: F should be a function type
// F extends (...args: any[]) => infer RT: the function return type is assigned to a variable RT
// infer RT ? RT: F: is RT a truth type? if yes return RT, otherwise return F

// the following test case can be pass
type cases = [
  // simple 1
  Expect<Equal<boolean, ReturnOf<() => boolean>>>,
  // simple 2
  Expect<Equal<123, ReturnOf<() => 123>>>,
  Expect<
    Equal<ComplexObject, ReturnOf<() => ComplexObject>>
  >,
  Expect<
    Equal<
      Promise<boolean>,
      ReturnOf<() => Promise<boolean>>
    >
  >,
  Expect<Equal<() => "foo", ReturnOf<() => () => "foo">>>,
  Expect<
    Equal<"heads" | "tails", ReturnOf<typeof flipCoin>>
  >,
  Expect<
    Equal<
      "rock" | "paper" | "scissors",
      ReturnOf<typeof rockPaperScissors>
    >
  >
]

 

Enforce the `F` to be a function type:

type ReturnOf<F> = F extends (...args: any[]) => infer RT ? RT: F
const person = { name: "Fred" };
// PersonType is {name: string}
type PersonType = ReturnOf<typeof person>

It would be nice to enforce ReturnOf only accept function as type:

type ReturnOf<F extends (...args: any[]) => any> = F extends (...args: any[]) => infer RT ? RT: F

 

Another way to write ReturnOf<F> is:

type ReturnOf<F extends (...args: any[]) => any> = F extends {(...args: any[]): infer RT} ? RT: never
// F extends {(): any}: F should be any callable
// F extends {(...args: any[]): infer RT}: the return type of the callable should be RT
// F extends {(...args: any[]): infer RT} ? RT: never : if the return type is true, then return RT otherwise return never

标签:RT,TypeScript,any,extends,ReturnOf,type,infer
来源: https://www.cnblogs.com/Answer1215/p/16494555.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有