# ts-types-utils **Repository Path**: weykon/ts-types-utils ## Basic Information - **Project Name**: ts-types-utils - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-08 - **Last Updated**: 2021-06-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ts-types-utils [![npm version](https://img.shields.io/npm/v/ts-types-utils.svg)](https://www.npmjs.com/package/ts-types-utils) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) Type utilities for typescript **Table of Contents** - [ts-types-utils](#ts-types-utils) - [Usage](#usage) - [Match](#match) - [MatchNames](#matchnames) - [Assign](#assign) - [Func](#func) - [PromiseOr](#promiseor) - [Action](#action) - [UndefinedToOptional](#undefinedtooptional) - [ArgsType ( DEPRECATED: already in std types as Parameters )](#argstype--deprecated-already-in-std-types-as-parameterst-) - [Related](#related) - [Contributors](#contributors) ## Usage ```sh npm i -D ts-type-utils ``` ```ts import * as TsTypeUtils from "ts-type-utils"; ``` ### Match `Match // 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 ```ts import { Match } from "ts-types-utils"; type FunctionProperties = Match; // Match type NonFunctionProperties = Match; type Foo = { a: string; b: number; c: () => void; d: (hello: string) => number; }; const nonfuncs: NonFunctionProperties; // { a: string, b: number } const funcs: FunctionProperties; // { c: () => void; d: (hello: string) => number } ``` ### MatchNames `MatchNames // 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 ```ts import { MatchNames } from "ts-types-utils"; type FunctionPropertiesNames = MatchNames; // MatchNames type NonFunctionPropertiesNames = MatchNames; type Foo = { a: string; b: number; c: () => void; d: (hello: string) => number; }; const nonfuncs: NonFunctionPropertiesNames; // "a" | "b" const funcs: FunctionPropertiesNames; // "c" | "d" ``` ### Assign `Assign` like A & B but replaces intersected A types with the ones from B ```ts 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 = { foo: 1, bar: 2, baz: false }; ``` ### Func `Func // P: params type, R: return type` This doesn't really bring much but syntactic sugar ```ts import { Func } from "ts-types-utils"; const myfunc: Func<[string, number], boolean>; // (a: string, b: number) => boolean ``` ### PromiseOr ```ts PromiseOr = T | Promise ``` 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). ```ts export function test(): Promise | string { ... } ``` Can be simplified to ```ts export function test(): PromiseOr { ... } ``` ### Action ```ts Action = () => Action ``` Why? Because it gets tiring and makes the code less clear having to write function types. ```ts 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 ```ts export function invoke(callback: Action)): void { ... } export function invoke(callback: Action): void { ... } export function invoke(callback: Action): Action { ... ``` ### UndefinedToOptional ```ts UndefinedToOptional ``` ```ts import { UndefinedToOptional } from "ts-types-utils"; interface Options { times: number | undefined } function foo(options: Options) {} foo({}) // Error expected times function bar(options: UndefinedToOptional) {} bar({}) // Good: times is optional now UndefinedToOptional // { times?: number } ``` ### ArgsType ( DEPRECATED: already in std types as Parameters ) `ArgsType // F is function` Like built-in `ReturnType` but for the args of a function, works for any number of arguments ```ts import { ArgsType } from "ts-types-utils"; function myFunc(a: string, b: number) {} const all: ArgsType; // [string, number] const first: ArgsType[0]; // string const second: ArgsType[1]; // number ``` ## Related Tests were made with [typescript-test-utils](https://github.com/danielpa9708/typescript-test-utils "typescript-test-utils") ## Contributors [![](https://img.shields.io/github/contributors/LeDDGroup/ts-types-utils.svg)](https://github.com/LeDDGroup/ts-types-utils/graphs/contributors) - **@danielpa9708** - **@cloudrex** - **@luisenrike**