// U is number
type U = ApiUnwrapPromise
```
## `ApiAwaitedReturn`
提取一个异步函数的返回类型。
```ts [Source]
export type ApiAwaitedReturn = TFn extends (...args: any[]) => ApiAwaitable ? R : never
```
```ts [Example]
import type { ApiAwaitedReturn } from '@movk/core'
type MyFn = () => Promise
// R is string
type R = ApiAwaitedReturn
```
## Changelog
:commit-changelog{prefix="types"}
# General
## `Suggest`
`Suggest` 类型允许您在获得特定字符串字面量(如 `'red' | 'blue'`)的 IDE 自动补全提示的同时,仍然可以接受任何其他字符串。
```ts [Source]
export type Suggest = T | (string & {})
```
```ts [Example]
import type { Suggest } from '@movk/core'
type Color = Suggest<'red' | 'green' | 'blue'>
const color1: Color = 'red' // IDE 会提示 'red', 'green', 'blue'
const color2: Color = 'yellow' // 也完全有效
```
## `ReactiveValue`
`ReactiveValue` 是 Vue 的 `MaybeRefOrGetter` 的扩展,它额外支持一个上下文参数。这在创建接收响应式数据或上下文相关回调的组件或组合式函数时非常有用。
```ts [Source]
export type ReactiveValue = [CTX] extends [never]
? MaybeRefOrGetter
: MaybeRefOrGetter | ((ctx: CTX) => T)
```
```ts [Example]
import type { ReactiveValue } from '@movk/core'
interface Context { user: { isAdmin: boolean } }
const visible: ReactiveValue = ctx => ctx.user.isAdmin
// 它可以是 ref, getter, 普通值, 或带上下文的函数
```
## `StripNullable`
从类型 `T` 中移除 `null` 和 `undefined`。
```ts [Source]
export type StripNullable = T extends null | undefined ? never : T
```
```ts [Example]
import type { StripNullable } from '@movk/core'
type MaybeString = string | null | undefined
// Result is string
type Result = StripNullable
```
## Changelog
:commit-changelog{prefix="types"}
# Object
## `OmitByKey`
依据键名从对象类型 `T` 中剔除键 `K`。
```ts [Source]
export type OmitByKey = {
[P in keyof T as P extends K ? never : P]: T[P];
}
```
```ts [Example]
interface User { id: string, name: string, age: number }
// R is { id: string; name: string }
type R = OmitByKey
```
## `PickByKey`
依据键名从对象类型 `T` 中挑选键 `K`。
```ts [Source]
export type PickByKey = {
[P in keyof T as P extends K ? P : never]: T[P];
}
```
```ts [Example]
interface User { id: string, name: string, age: number }
// R is { id: string; name: string }
type R = PickByKey
```
## `RenameKeys`
基于映射表 `Mapping` 对对象类型 `T` 的键进行重命名。
```ts [Source]
export type RenameKeys = {
[K in keyof T as K extends keyof Mapping ? Exclude : K]: T[K];
}
```
```ts [Example]
interface Src { a: number, b: string }
// R is { id: number; b: string }
type R = RenameKeys
```
## `RequiredByKeys`
将对象类型 `T` 中的键 `K` 标记为必填。
```ts [Source]
export type RequiredByKeys = T & {
[P in K]-?: T[P];
}
```
```ts [Example]
interface User { id: string, name?: string }
// R['name'] is required string
type R = RequiredByKeys
```
## `PartialByKeys`
将对象类型 `T` 中的键 `K` 标记为可选。
```ts [Source]
export type PartialByKeys = Omit & Partial>
```
```ts [Example]
interface User { id: string, name: string }
// R['name'] is optional
type R = PartialByKeys
```
## `ReadonlyByKeys`
将对象类型 `T` 中的键 `K` 标记为只读。
```ts [Source]
export type ReadonlyByKeys = T & {
readonly [P in K]: T[P];
}
```
## `MutableByKeys`
取消对象类型 `T` 中键 `K` 的只读限制。
```ts [Source]
export type MutableByKeys = {
-readonly [P in K]: T[P];
} & Omit
```
## `UnionToIntersection`
将联合类型 `U` 转换为交叉类型。
```ts [Source]
export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? { [K in keyof I]: I[K]; } : never
```
## `FirstParam`
若对象 `T` 在键 `K` 处的类型为元组,则提取其首个元素类型。
```ts [Source]
export type FirstParam = T[K] extends [infer P, ...any[]] ? P : never
```
## `FirstParameter`
从函数类型中提取首个参数类型。
```ts [Source]
export type FirstParameter = T extends (arg: infer P, ...args: any[]) => any ? P : undefined
```
## `DeepPartial`
递归将对象类型 `T` 的所有属性变为可选。
```ts [Source]
export type DeepPartial = {
[P in keyof T]?: T[P] extends object ? DeepPartial : T[P] | undefined;
}
```
```ts [Example]
interface Src { a: { b: number } }
// R is { a?: { b?: number | undefined } | undefined }
type R = DeepPartial
```
## `GetObjectField`
当 `MaybeObject` 为对象时,返回键 `Key` 对应的属性类型。
```ts [Source]
export type GetObjectField = MaybeObject extends Record ? MaybeObject[Key] : never
```
## `StringOrVNode`
一个方便的类型,它代表一个可以是字符串、一个 VNode 对象,或一个返回 VNode 的函数。
```ts [Source]
export type StringOrVNode = string | VNode | (() => VNode)
```
## `Merge`
合并两个对象类型,`U` 中的属性会覆盖 `T` 中的属性。
```ts [Source]
export type Merge = Omit & U
```
## `IsPlainObject`
判断类型 `T` 是否为纯对象类型。
```ts [Source]
export type IsPlainObject = (T extends null | undefined ? never : T) extends Record ? (T extends null | undefined ? never : T) extends any[] ? false : (T extends null | undefined ? never : T) extends (...args: any[]) => any ? false : (T extends null | undefined ? never : T) extends Date ? false : true : false
```
## `NestedKeys`
提取对象的嵌套键,支持点语法路径。
```ts [Source]
export type NestedKeys = [D] extends [never] ? never : { [K in keyof T & string]: IsPlainObject extends true ? K | `${K}.${NestedKeys}` : K }[keyof T & string]
```
```ts [Example]
interface User {
name: string
address: {
city: string
country: string
}
}
// Keys is 'name' | 'address' | 'address.city' | 'address.country'
type Keys = NestedKeys
```
## `ObjectFieldKeys`
提取对象中所有纯对象字段的键(包括嵌套的),支持点语法路径。
```ts [Source]
export type ObjectFieldKeys = [D] extends [never] ? never : { [K in keyof T & string]: IsPlainObject extends true ? K | `${K}.${ObjectFieldKeys}` : never }[keyof T & string]
```
## `NonObjectFieldKeys`
提取对象中所有非对象字段的键。
```ts [Source]
export type NonObjectFieldKeys = Exclude, ObjectFieldKeys>
```
## `ArrayFieldKeys`
提取对象中所有数组字段的键(包括嵌套的),支持点语法路径。
```ts [Source]
export type ArrayFieldKeys = [D] extends [never] ? never : { [K in keyof T & string]: (T[K] extends null | undefined ? never : T[K]) extends any[] ? K : IsPlainObject extends true ? `${K}.${ArrayFieldKeys}` : never }[keyof T & string]
```
## `GetFieldValue`
根据路径字符串提取对象属性的类型。
```ts [Source]
export type GetFieldValue = P extends keyof T ? T[P] : P extends `${infer K}.${infer Rest}` ? K extends keyof T ? T[K] extends undefined ? undefined : GetFieldValue, Rest> : unknown : unknown
```
```ts [Example]
interface User {
name: string
tags: string[]
profile: {
bio: string
}
}
// T1 is string[]
type T1 = GetFieldValue
// T2 is string
type T2 = GetFieldValue
```
## Changelog
:commit-changelog{prefix="types/object"}
# Storage
## `StorageType`
表示浏览器存储类型的字符串字面量类型。
```ts [Source]
export type StorageType = 'localStorage' | 'sessionStorage'
```
```ts [Example]
const storage: StorageType = 'localStorage'
```
## `StorageConfig`
`useAppStorage` 函数的完整配置对象类型。
```ts [Source]
export interface StorageConfig {
key: string
defaultValue: T
prefix: string
storage: StorageType
}
```
### 属性说明
- `key`: 存储项的唯一键名
- `defaultValue`: 默认值
- `prefix`: 键名前缀,用于创建命名空间
- `storage`: 存储类型,`localStorage` 或 `sessionStorage`
## `StorageConfigInput`
`useAppStorage` 函数的输入配置对象类型,`prefix` 和 `storage` 为可选参数。
```ts [Source]
export type StorageConfigInput = Partial, 'key' | 'defaultValue'>> & {
key: string
defaultValue: T
}
```
```ts [Example]
import type { StorageConfigInput } from '@movk/core'
import { useAppStorage } from '@movk/core'
const config: StorageConfigInput<{ theme: string }> = {
key: 'user-preferences',
defaultValue: { theme: 'light' },
storage: 'localStorage', // 可选
prefix: 'app' // 可选
}
const storage = useAppStorage(config)
```
## `AppStorageReturn`
`useAppStorage` 函数的返回对象接口。
```ts [Source]
export interface AppStorageReturn {
state: Ref
getItem: () => T
setItem: (value: T) => void
removeItem: () => void
}
```
### 属性说明
- `state`: 响应式引用,与存储数据同步
- `getItem`: 从存储中读取数据
- `setItem`: 设置新值到存储
- `removeItem`: 从存储中移除项
```ts [Example]
import type { AppStorageReturn } from '@movk/core'
import { useAppStorage } from '@movk/core'
const storage: AppStorageReturn = useAppStorage({
key: 'my-key',
defaultValue: 'hello'
})
// 使用响应式状态
console.log(storage.state.value) // 'hello'
// 更新值
storage.setItem('world')
// 读取值
const value = storage.getItem() // 'world'
// 移除项
storage.removeItem()
```
## Changelog
:commit-changelog{prefix="types"}
# URL
## `ParsedUrl`
URL 解析结果接口。
```ts
interface ParsedUrl {
/** 完整的原始 URL */
href: string
/** 协议 (http:, https:, etc.) */
protocol: string
/** 主机名 + 端口 */
host: string
/** 主机名 */
hostname: string
/** 端口号 */
port: string
/** 路径部分 */
pathname: string
/** 查询字符串 (包含 ?) */
search: string
/** 哈希部分 (包含 #) */
hash: string
/** 用户认证信息 (user:pass) */
auth: string
/** 源 (protocol + host) */
origin: string
}
```
## 用法
```ts
import type { ParsedUrl } from '@movk/core'
import { parseUrl } from '@movk/core'
const result: ParsedUrl | null = parseUrl('https://example.com/path')
```
## `QueryParamValue`
查询参数值类型。
```ts
type QueryParamValue = string | number | boolean | null | undefined
```
## 用法
```ts
import type { QueryParamValue } from '@movk/core'
const value: QueryParamValue = 'hello'
const numValue: QueryParamValue = 123
const boolValue: QueryParamValue = true
```
## `QueryParams`
查询参数对象类型。
```ts
type QueryParams = Record
```
## 用法
```ts
import type { QueryParams } from '@movk/core'
const params: QueryParams = {
page: 1,
limit: 10,
tags: ['a', 'b', 'c'],
active: true,
}
```
## Changelog
:commit-changelog{prefix="types/url"}
# Vue
这些类型主要用于在 TypeScript 环境下更精确地推断 Vue 组件的 `props`、`slots`、`emits` 等。
## `ComponentProps`
从一个组件类型 `T` 中提取其 `props` 类型。
```ts [Source]
export type ComponentProps = T extends new (...args: any) => { $props: infer P } ? NonNullable
: T extends (props: infer P, ...args: any) => any ? P
: {}
```
```ts [Example]
import MyComponent from './MyComponent.vue'
type Props = ComponentProps
// Props will be the type of MyComponent's props
```
## `ComponentSlots`
从一个组件类型 `T` 中提取其 `slots` 类型。
```ts [Source]
export type ComponentSlots = T extends new (...args: any) => { $slots: infer S } ? NonNullable
: T extends (props: any, ctx: { slots: infer S, attrs: any, emit: any }, ...args: any) => any ? NonNullable
: {}
```
## `ComponentAttrs`
从一个组件类型 `T` 中提取其 `attrs` 类型。
```ts [Source]
export type ComponentAttrs = T extends new (...args: any) => { $attrs: infer A } ? NonNullable
: T extends (props: any, ctx: { slots: any, attrs: infer A, emit: any }, ...args: any) => any ? NonNullable
: {}
```
## `ComponentEmit`
从一个组件类型 `T` 中提取其 `emit` 函数的类型。
```ts [Source]
export type ComponentEmit = T extends new (...args: any) => { $emit: infer E } ? NonNullable
: T extends (props: any, ctx: { slots: any, attrs: any, emit: infer E }, ...args: any) => any ? NonNullable
: {}
```
## `ComponentExposed`
从一个组件类型 `T` 中提取其 `expose` 的类型。
```ts [Source]
export type ComponentExposed = T extends new (...args: any) => infer E ? E
: T extends (props: any, ctx: any, expose: (exposed: infer E) => any, ...args: any) => any ? NonNullable
: {}
```
## Changelog
:commit-changelog{prefix="types"}
# 版本发布