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

ts-types-utils

v1.5.0

Published

Type utilities for typescript

Downloads

69

Readme

ts-types-utils

npm version Conventional Commits code style: prettier

Type utilities for typescript

Table of Contents

Usage

npm i -D ts-type-utils
import * as TsTypeUtils from "ts-type-utils";

Match

Match<T, M, F> // T is object, M is what to match to, F negate

Pick from T all properties that match M, or not match M if F is false

import { Match } from "ts-types-utils";

type FunctionProperties<T> = Match<T, Function>; // Match<T, Function, true>
type NonFunctionProperties<T> = Match<T, Function, false>;

type Foo = {
  a: string;
  b: number;
  c: () => void;
  d: (hello: string) => number;
};

const nonfuncs: NonFunctionProperties<Foo>; // { a: string, b: number }
const funcs: FunctionProperties<Foo>; // { c: () => void; d: (hello: string) => number }

MatchNames

MatchNames<T, M, F> // T is object, M is what to match to, F negate

Get properties names from T that match M, or not match M if F is false

import { MatchNames } from "ts-types-utils";

type FunctionPropertiesNames<T> = MatchNames<T, Function>; // MatchNames<T, Function, true>
type NonFunctionPropertiesNames<T> = MatchNames<T, Function, false>;

type Foo = {
  a: string;
  b: number;
  c: () => void;
  d: (hello: string) => number;
};

const nonfuncs: NonFunctionPropertiesNames<Foo>; // "a" | "b"
const funcs: FunctionPropertiesNames<Foo>; // "c" | "d"

Assign

Assign<A, B> like A & B but replaces intersected A types with the ones from B

import { Assign } from "ts-types-utils";

type A = {
  foo: string;
  bar: number;
};
type B = {
  foo: number;
  baz: boolean;
};

const bad: A & B = {
  foo: 1, // error type string | number doesn't match number
  bar: 2,
  baz: false
};
const good: Assign<A, B> = {
  foo: 1,
  bar: 2,
  baz: false
};

Func

Func<P, R> // P: params type, R: return type

This doesn't really bring much but syntactic sugar

import { Func } from "ts-types-utils";

const myfunc: Func<[string, number], boolean>; // (a: string, b: number) => boolean

PromiseOr

PromiseOr<T> = T | Promise<T>

Why? Because it gets cumbersome having to repeat long types again and again. I've found myself using this a lot, specially in interfaces (for greater flexibility).

export function test(): Promise<string> | string { ... }

Can be simplified to

export function test(): PromiseOr<string> { ... }

Action

Action<T = void> = () => Action

Why? Because it gets tiring and makes the code less clear having to write function types.

export function invoke(callback: () => string)): void { ... }

export function invoke(callback: () => string | number | object)): void { ... }

export function invoke(callback: () => boolean | object)): () => boolean | string { ... }

Can be simplified to

export function invoke(callback: Action<string>)): void { ... }

export function invoke(callback: Action<string | number | object>): void { ... }

export function invoke(callback: Action<boolean | object>):  Action<boolean | string> { ...

UndefinedToOptional

UndefinedToOptional<T>
import { UndefinedToOptional } from "ts-types-utils";

interface Options {
  times: number | undefined
}
function foo(options: Options) {}
foo({}) // Error expected times

function bar(options: UndefinedToOptional<Options>) {}
bar({}) // Good: times is optional now

UndefinedToOptional<Options> // { times?: number }

ArgsType ( DEPRECATED: already in std types as Parameters )

ArgsType<F> // F is function

Like built-in ReturnType but for the args of a function, works for any number of arguments

import { ArgsType } from "ts-types-utils";
function myFunc(a: string, b: number) {}
const all: ArgsType<typeof myFunc>; // [string, number]
const first: ArgsType<typeof myFunc>[0]; // string
const second: ArgsType<typeof myFunc>[1]; // number

Related

Tests were made with typescript-test-utils

Contributors

  • @danielpa9708
  • @cloudrex
  • @luisenrike