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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jl-org/ts-tool

v0.0.8

Published

为 Typescript 扩展的实用泛型工具,以及类型体操

Downloads

65

Readme

TS 实用泛型工具

安装

npm i -D @jl-org/ts-tool

目录

全都有文档注释

type O = {
    a: string
    b: number
    c: boolean
    d: {
        e: {
            f: any
        }
    }
}

type Arr = [string, number, boolean, { e: { f: any } }]

type NestedPromise = Promise<Promise<Promise<string>>>

工具

import type {
    DeepReadonly,
    DeepPartial,
    DeepRequired,
    RemoveReadonly,

    UnPacked,
    Optional,
    PartRequired,

    Getters,
    Setters,
    GetterAndSetters,

    PickReadonlyKeys,
    PickPropType,
} from '@jl-org/ts-tool'


/* 部分可选 */
type PartialO = Optional<O, 'a'>
/* 部分必填 */
type RequiredO = PartRequired<O, 'a'>

/** 深度只读 */
type DeepReadonlyO = DeepReadonly<O>
/** 去除 readonly */
type RemoveReadonlyO = RemoveReadonly<DeepReadonlyO>

/** 深度可选 */
type DeepPartialO = DeepPartial<O>
/** 深度必选 */
type DeepRequiredO = DeepRequired<O>

/** ======================================================== */

/** 究极解包,支持获取`数组 | 函数返回值 | Promise<Promise<...>>` 里的类型 */
type GetArrType = UnPacked<Arr>  // string | number | boolean | { e: { f: any } }
type GetPromiseType = UnPacked<NestedPromise>  // string
type GetFuncType = UnPacked<() => string>  // string

/** ======================================================== */

/**
 * 下面就是类型体操范畴了
 */

/** Getters */
type GenGetters = Getters<O>
/** Setters */
type GenSetters = Setters<O>
/** GetterAndSetters */
type GenGetterAndSetters = GetterAndSetters<O>

/** 取出只读的键 */
type PickReadonlyKeysO = PickReadonlyKeys<Readonly<O>>  // "a" | "b" | "c" | "d"

/** 取出结构体某个类型 */
type PickPropTypeO = PickPropType<O, 'd.e'>  // { f: any }

/** 获取对象的所有属性,排除函数 */
type PickAllPropO = PickProps<O>

数组工具

import type {
    FirstArr,
    PopArr,
    ReverseArr,
} from '@jl-org/ts-tool'


/** 首个元组类型 */
type First = FirstArr<Arr>  // string

/** 删除元组末尾类型 */
type Pop = PopArr<Arr>  // [string, number, boolean]

/* 元组反转 */
type Reverse = ReverseArr<Arr>  // [{ e: { f: any } }, boolean, number, string]

字符串工具

import type {
    SplitStr,
    GetStrLen,
    StartsWith,
    PickStartsWith,
    QueryKV,
    CamelCase,
    ToSnake,
} from '@jl-org/ts-tool'


/** 是否以 `xx` 开头 */
type StartsWithX = StartsWith<'apple', 'a'>  // apple
/** 取出以 `xx` 开头的键值对,例如取出事件 */
type PickStartsWithX = PickStartsWith<{
    onChange: () => void
    onClick: () => void
    other: () => void
}, 'on'>

/** 把 Query 字符串,转为键值对 */
type QueryKVType = QueryKV<'name=kn&sex=f&language=ts'>

/** 蛇形转驼峰 */
type CamelCaseStr = CamelCase<'hello_world'>  // helloWorld
/** 驼峰转蛇形 */
type ToSnakeStr = ToSnake<'helloWorld'>  // hello_world

/** 获取字符串长度 */
type StrLen = GetStrLen<'hello'>  // 5

/** 字符串分割成联合类型 */
type Split = SplitStr<'abc'>  // 'a' | 'b' | 'c'