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

@mxnet/types

v0.1.4

Published

types

Downloads

1

Readme

types

常用类型声明和类型测试

Usage

import types from "@mxnet/types";

Test

import { Extends, Expect, Equal, IsAny, IsFalse } from "@mxnet/types";
// 检查类型是否为 true, 不为 true 时 编译器会报错, 通常搭配 Equal 使用
type result = Expect<false>;

// Equal 接受两个泛型参数 返回 boolean 传入 Expect 泛型 由编译器检查类型
type result1 = Expect<Equal<string, boolean>>;

// IsAny 检查是否类型为 any
type s = IsAny<number>; // false
// 检查 类型 是否 互为子集
type s = Extends<son, father>; // boolean

// IsFalse 检查类型是否为 false
type s = IsFalse<false>; // true

types

  • fn 定义一个普通函数

    • T 定义函数返回值
export type fn<T = unknown> = (...args: any[]) => T;
// 泛型参数为函数的返回值
type f = fn<number>;
const a: f = () => {
  return 1;
};
  • promiseFn 定义一个 promise 函数

    • T 定义函数返回值
export type promiseFn<T = unknown> = (...args: any[]) => Promise<T>;
// 泛型参数为函数的返回值
type f = promiseFn<number>;
const a: f = async () => {
  return 1;
};
  • nonNullFnParameter 获取一个函数参数 且 绝对不为空值

    • T 传入函数
export type nonNullFnParameter<T extends (...args: any) => any> = NonNullable<
  Parameters<T>
>;
const a = (n: number, l: string) => {};
type s = nonNullFnParameter<typeof a>; // [n:number, l:string];

const l = (...k: s) => {};
l(1, "");
  • mergeFnWithPromiseFn 普通函数和 promise 函数的交叉类型

    • T 函数返回值类型 默认 unknown

    • P . ArrayLike形式的函数参数 默认(any)

    • isP 是否返回 Promise 函数 , truefalse 不是 , 默认 undefined 返回 普通函数和 promise 函数

export type mergeFnWithPromiseFn<
  T = unknown,
  P extends any[] = any,
  isP extends boolean | undefined = undefined
> = isP extends undefined
  ? fn<T, P> | promiseFn<T, P>
  : isP extends true
  ? promiseFn<T, P>
  : fn<T, P>;

const f = (n: mergeFnWithPromiseFn<void, [], undefined>) => {};

f(async () => {}); // success

f(() => {}); // success

const f = (n: mergeFnWithPromiseFn<void, [], false>) => {};
f(() => {}); // success
f(async () => {}); // error , 这里需要的是一个 普通函数类型 类型
  • isPromiseFn 需要一个泛型函数 判断是否为 Promise 函数 , 返回结果 boolean

    • f 传入函数
export type isPromiseFn<f extends mergeFnWithPromiseFn<any, any, undefined>> =  Equal<ReturnType<f>, Promise<any>>;

type s = isPromiseFn(() => {}); // false
type a = isPromiseFn(async () => {}) // false