API

一组用于处理异步函数和 Promise 的工具类型。

ApiAwaitable<T>

统一同步和异步操作的返回类型。

Source
export type ApiAwaitable<T> = T | Promise<T>
Example
import type { ApiAwaitable } from '@movk/core'

// a: string | Promise<string>
let a: ApiAwaitable<string>

ApiUnwrapPromise<T>

提取 Promise 中包含的类型。

Source
export type ApiUnwrapPromise<T> = T extends Promise<infer U> ? U : T
Example
import type { ApiUnwrapPromise } from '@movk/core'

type P = Promise<number>

// U is number
type U = ApiUnwrapPromise<P>

ApiAwaitedReturn<TFn>

提取一个异步函数的返回类型。

Source
export type ApiAwaitedReturn<TFn> = TFn extends (...args: any[]) => ApiAwaitable<infer R> ? R : never
Example
import type { ApiAwaitedReturn } from '@movk/core'

type MyFn = () => Promise<string>

// R is string
type R = ApiAwaitedReturn<MyFn>

Changelog

03580 — feat: 统一同步/异步返回类型并补充文档注释

6f6c7 — chore: 更新 CI 配置和新增类型定义

Copyright © 2024 - 2025 YiXuan - MIT License