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 🙏

© 2024 – Pkg Stats / Ryan Hefner

hry-types

v0.18.1

Published

typescript工具类型库

Downloads

60

Readme

简介

特点

  • 项目中注释和文档等全部使用中文。
  • 泛型分类,方便查找。
  • 类型编译速度更快。

一些思想

  • 为特定类型而生 每个分类下的泛型都应只为此分类而建立的。因为使用者很明确使用泛型的场景。例如,编写一个针对对象类型的Filter泛型时,不应该考虑传入非对象类型时的场景。测试用例同样。
  • 减少泛型的不必要约束 因为泛型都是针对特定类型的,所以参数可以无需写泛型约束,这在复杂类型推导时,可以避免写过多的 'extends'用以缩窄类型。
    // 示例A
    import { type N } from "hry-types";
    type Return<T extends string | number> = T extends `${T}1` ? T : N.Add<T, 1>;
    function foo<T extends string | number>(p: T): Return<T> {
      return p + 1;
    }
    const test = foo(1); // test => 2
    示例A中使用了N(number)类别中的泛型Add。 当你写Return类型的Add部分时,你一定知道传入T类型必为number,若写Add泛型时加入了一参的泛型约束(number),那如上写法ts会报错(Add位置),因为ts认为传入T类型为 string | number,导致你要多写一个" extends number ? N.Add<T,1> : never " 来缩窄传入的T类型。

安装

npm install hry-types -D

tsconfig

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "types": ["./node_modules/hry-types", "other-types"]
  }
}

使用示例

import { type A } from "hry-types";
import type { IfEquals } from "hry-types/src/Any/IfEquals";

type foo<T extends string | number> = A.IfEquals<T, string, "string", "number">;
// type foo<T extends string | number> = IfEquals<T, string, "string", "number">

type test = foo<string>; // test => 'string'

type test2 = foo<number>; // test2 => 'number'