欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解typescript中的infer關(guān)鍵字的使用

 更新時間:2021年06月27日 10:27:18   作者:ESnail  
infer 這個關(guān)鍵字,整理記錄一下,避免后面忘記了。具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

infer 這個關(guān)鍵字,整理記錄一下,避免后面忘記了。有點難以理解呢。

infer

infer 是在 typescript 2.8中新增的關(guān)鍵字。

infer 可以在 extends 條件類型的字句中,在真實分支中引用此推斷類型變量,推斷待推斷的類型。

例如:用infer推斷函數(shù)的返回值類型

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;

type fn = () => number
type fnReturnType = ReturnType<fn> // number

在這個例子中,

T extends U ? X : Y的形式為條件類型。

infer R代表待推斷的返回值類型,如果T是一個函數(shù)(...args: any[]) => infer R,則返回函數(shù)的返回值R,否則返回any

案例:加深理解

反解 Promise

// promise 響應(yīng)類型
type PromiseResType<T> = T extends Promise<infer R> ? R : T

// 驗證
async function strPromise() {
  return 'string promise'
}

interface Person {
  name: string;
  age: number;
}
async function personPromise() {
  return {
    name: 'p',
    age: 12
  } as Person
}

type StrPromise = ReturnType<typeof strPromise> // Promise<string>
// 反解
type StrPromiseRes = PromiseResType<StrPromise> // str

type PersonPromise = ReturnType<typeof personPromise> // Promise<Person>
// 反解
type PersonPromiseRes = PromiseResType<PersonPromise> // Person

反解函數(shù)入?yún)㈩愋?br />

type Fn<A extends any[]> = (...args: A) => any
type FnArgs<T> = T extends Fn<infer A> ? A : any

function strFn (name: string) {

}

type StrFn = FnArgs<typeof strFn> // [string]

tuple 轉(zhuǎn) union ,如:[string, number] -> string | number

type ElementOf<T> = T extends Array<infer E> ? E : never

type TTuple = [string, number];

type ToUnion = ElementOf<ATuple>; // string | number

new 操作符

// 獲取參數(shù)類型
type ConstructorParameters<T extends new (...args: any[]) => any> = T extends new (...args: infer P) => any ? P : never;

// 獲取實例類型
type InstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R ? R : any;

class TestClass {

  constructor(
    public name: string,
    public string: number
  ) {}
}

type Params = ConstructorParameters<typeof TestClass>;  // [string, numbder]

type Instance = InstanceType<typeof TestClass>;         // TestClass

react - reducer

// 定義
function useReducer<R extends Reducer<any, any>, I>(
  reducer: R,
  // ReducerState 推斷類型
  initializerArg: I & ReducerState<R>,
  initializer: (arg: I & ReducerState<R>) => ReducerState<R>
): [ReducerState<R>, Dispatch<ReducerAction<R>>];

// infer推斷
type ReducerState<R extends Reducer<any, any>> = R extends Reducer<infer S, any>
  ? S
  : never;
// Reducer類型
type Reducer<S, A> = (prevState: S, action: A) => S;


// 使用 reducer
const reducer = (x: number) => x + 1;
const [state, dispatch] = useReducer(reducer, '');
// Argument of type "" is not assignable to parameter of type 'number'.

vue3 - ref

export interface Ref<T = any> {
  [isRefSymbol]: true
  value: T
}

export function ref<T>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>

export type UnwrapRef<T> = {
  cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
  ref: T extends Ref<infer V> ? UnwrapRef<V> : T
  array: T
  object: { [K in keyof T]: UnwrapRef<T[K]> }
}[T extends ComputedRef<any>
  ? 'cRef'
  : T extends Array<any>
    ? 'array'
    : T extends Ref | Function | CollectionTypes | BaseTypes
      ? 'ref' // bail out on types that shouldn't be unwrapped
      : T extends object ? 'object' : 'ref']


// 使用
const count = ref({
  foo: ref('1'),
  bar: ref(2)
})

// 推斷出
const count: Ref<{
  foo: string;
  bar: number;
}>

const count = ref(2) // Ref<number>

const count = ref(ref(2)) // Ref<number>

參考

理解TypeScript中的infer關(guān)鍵字

Vue3 跟著尤雨溪學(xué) TypeScript 之 Ref 類型從零實現(xiàn)

巧用 TypeScript(五)---- infer

到此這篇關(guān)于深入理解typescript中的infer關(guān)鍵字的使用的文章就介紹到這了,更多相關(guān)typescript infer關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript 數(shù)組去重詳解

    JavaScript 數(shù)組去重詳解

    下面小編就為大家?guī)硪黄狫avaScript數(shù)組去重的幾方法推薦。小編覺得聽錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看
    2021-09-09
  • Webpack實戰(zhàn)加載SVG的方法

    Webpack實戰(zhàn)加載SVG的方法

    本篇文章主要介紹了Webpack實戰(zhàn)加載SVG的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 利用js+canvas實現(xiàn)掃雷游戲

    利用js+canvas實現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了利用js+canvas實現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • HTML顏色選擇器實現(xiàn)代碼

    HTML顏色選擇器實現(xiàn)代碼

    HTML顏色選擇器實現(xiàn)代碼需要的朋友可以參考下。
    2010-11-11
  • 奇偶行高亮顯示及鼠標(biāo)劃過高亮顯示類

    奇偶行高亮顯示及鼠標(biāo)劃過高亮顯示類

    奇或偶數(shù)行高亮顯示及鼠標(biāo)莫過高亮顯示,一個經(jīng)常用到的效果,也能谷歌到大把的這種效果JS,但好像還沒有一個封裝成類直接用的.想象自己當(dāng)初谷歌這個類時,還真沒少折騰時間.
    2010-07-07
  • 淺析Javascript中雙等號(==)隱性轉(zhuǎn)換機制

    淺析Javascript中雙等號(==)隱性轉(zhuǎn)換機制

    這篇文章給大家詳細(xì)介紹了javascript中雙等號(==)隱性轉(zhuǎn)換機制,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-10-10
  • JS設(shè)置cookie、讀取cookie

    JS設(shè)置cookie、讀取cookie

    js設(shè)置cookie有很多種方法,包括JS設(shè)置cookie、讀取cookie,工作中常會用到!下面是詳細(xì)代碼,感興趣的小伙伴們可以參考一下
    2016-02-02
  • JavaScript Event學(xué)習(xí)第十章 一些可替換的事件對

    JavaScript Event學(xué)習(xí)第十章 一些可替換的事件對

    為了讓我們的JavaScript驅(qū)動的頁面對那些不能或者不想使用鼠標(biāo)的用戶也能很好的使用,我們對于像mouseover和click這樣的事件做一些處理,同樣的,對于非鼠標(biāo)事件也同樣的要我們的腳本執(zhí)行。
    2010-02-02
  • JS電梯導(dǎo)航的實現(xiàn)示例

    JS電梯導(dǎo)航的實現(xiàn)示例

    本文主要介紹了JS電梯導(dǎo)航的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 微信小程序返回到頂部功能的簡單實現(xiàn)

    微信小程序返回到頂部功能的簡單實現(xiàn)

    在做微信小程序開發(fā)時,遇到一個問題,要如何實現(xiàn)返回頂部的功能,下面這篇文章主要給大家介紹了微信小程序返回到頂部功能的簡單實現(xiàn),文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11

最新評論