Code Monkey home page Code Monkey logo

ts-types-utils's Introduction

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

ts-types-utils's People

Contributors

danielpza avatar dependabot[bot] avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ts-types-utils's Issues

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.1 to 24.0.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.9 to 24.0.10.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.