npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tker/type

v1.0.1

Published

TypeScript 常用类型定义集合,提供实用的工具类型和基础类型定义。

Downloads

174

Readme

@tker/type

TypeScript 常用类型定义集合,提供实用的工具类型和基础类型定义。

安装

pnpm add @tker/type

使用方式

import type { DeepPartial, Recordable, AnyFunction } from '@tker/type'

// 使用 DeepPartial
interface Config {
  name: string
  options: {
    debug: boolean
    port: number
  }
}

const partialConfig: DeepPartial<Config> = {
  name: 'test',
  options: {
    debug: true  // port 可选
  }
}

// 使用 Recordable
const data: Recordable<string> = {
  key1: 'value1',
  key2: 'value2'
}

// 使用 AnyFunction
type Handler = AnyFunction<[string, number], boolean>

类型定义

工具类型

| 类型 | 说明 | |------|------| | DeepPartial<T> | 深层递归所有属性为可选 | | DeepReadonly<T> | 深层递归所有属性为只读 | | Merge<O, T> | 合并两个对象类型,后者覆盖前者 | | MergeAll<T> | 合合数组中的所有对象类型 | | MaybePromise<T> | 同步或异步返回值类型 |

函数类型

| 类型 | 说明 | |------|------| | AnyFunction<T, R> | 任意类型的函数(同步或异步) | | AnyNormalFunction<T, R> | 任意类型的普通函数 | | AnyPromiseFunction<T, R> | 任意类型的异步函数 | | EmitType | 事件发射函数类型 |

包装类型

| 类型 | 说明 | |------|------| | Nullable<T> | null \| T,表示可能为 null 的类型 | | NonNullable<T> | 从 T 中排除 nullundefined | | Recordable<T> | 字符串键对象类型 Record<string, T> | | ReadonlyRecordable<T> | 只读字符串键对象类型 |

其他类型

| 类型 | 说明 | |------|------| | TimeoutHandle | setTimeout 返回值类型 | | IntervalHandle | setInterval 返回值类型 | | ClassType | CSS 类名类型,支持多种形式 |

基础选项类型

| 类型 | 说明 | |------|------| | BasicOption | 基础选项 { label: string; value: string } | | SelectOption | 选择器选项(等同于 BasicOption) | | TabOption | 标签页选项(等同于 BasicOption) |

详细示例

DeepPartial

深层可选,适用于配置对象:

import type { DeepPartial } from '@tker/type'

interface AppConfig {
  server: {
    host: string
    port: number
    ssl: {
      enabled: boolean
      cert: string
    }
  }
  database: {
    host: string
    name: string
  }
}

// 所有层级属性都可选
const config: DeepPartial<AppConfig> = {
  server: {
    port: 3000
    // ssl 可以完全省略
  }
}

Merge 和 MergeAll

合并对象类型:

import type { Merge, MergeAll } from '@tker/type'

// Merge - 合并两个类型
type Base = { name: string; age: number }
type Extra = { age: string; sex: 'male' | 'female' }

type Result = Merge<Base, Extra>
// { name: string; age: string; sex: 'male' | 'female' }
// age 被 Extra 覆盖为 string

// MergeAll - 合合数组中的所有类型
type Configs = [
  { name: string; age: number },
  { sex: 'male' | 'female'; age: string },
  { id: number }
]

type AllResult = MergeAll<Configs>
// { name: string; sex: 'male' | 'female'; age: string; id: number }

AnyFunction

灵活的函数类型定义:

import type { AnyFunction, AnyPromiseFunction } from '@tker/type'

// 接收任意函数
function registerHandler(handler: AnyFunction) {
  // handler 可以是同步或异步
}

registerHandler(() => 'sync')
registerHandler(async () => 'async')

// 明确要求异步函数
type AsyncHandler = AnyPromiseFunction<[string], number>
const handler: AsyncHandler = async (input) => input.length

Nullable 和 NonNullable

处理可能为 null 的值:

import type { Nullable, NonNullable } from '@tker/type'

// Nullable - 表示可能为 null
interface User {
  id: number
  name: string
  email: Nullable<string>  // email 可能为 null
}

const user1: User = { id: 1, name: 'Alice', email: '[email protected]' }
const user2: User = { id: 2, name: 'Bob', email: null }

// NonNullable - 排除 null 和 undefined
type DefinedValue = NonNullable<string | null | undefined>
// 结果:string(联合类型分配律,分别排除 null 和 undefined)

type EmptyResult = NonNullable<null | undefined>
// 结果:never(没有非空类型时)

function processValue(value: Nullable<string>): NonNullable<string> {
  if (value === null) {
    return 'default'
  }
  return value  // 这里 value 已经是 string 类型
}

Recordable

动态键值对象:

import type { Recordable } from '@tker/type'

// 字符串值映射
const envMap: Recordable<string> = {
  NODE_ENV: 'production',
  API_URL: 'https://api.example.com'
}

// 复杂类型映射
const configMap: Recordable<{ enabled: boolean; value: any }> = {
  featureA: { enabled: true, value: 100 },
  featureB: { enabled: false, value: null }
}

ClassType

CSS 类名多种形式:

import type { ClassType } from '@tker/type'

// 支持多种形式
const class1: ClassType = 'btn-primary'
const class2: ClassType = { 'btn-active': true, 'btn-disabled': false }
const class3: ClassType = ['btn', 'btn-large', { 'btn-round': true }]

License

MIT