Neural Processing Mechanisms
Wondering what’s next for npm?Check out our public roadmap! »

typescript-utility-types
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

typescript-utility-types

Collection of generic types for TypeScript, complementing built-in mapped types and aliases.

Installation

  1. Install package
npm install --save-dev typescript-utility-types
  1. Add reference to your <type definition file>.d.ts
/// <reference types="typescript-utility-types"/>

Or:

import 'typescript-utility-types';

Typescript compatibility

  • v3 - minimum TS v3.6.0

Table of content

Dictionary

Dictionary is variable object type.

Usage:

  let obj: Dictionary;
  obj = { a: 0, b: 0 }; // Ok
  obj = { c: 'string', d: 'string' }; // Ok

XOR<A, B>

XOR will create exclusive set where you can set only one or other type

Usage:

  type exclusive = XOR<{ property: string }, { property2: number }>;
 
  let result: exclusive;
  result = { property: 'test' }; // Ok
  result = { property2: 1 }; // Ok
  result = { property: 'test', property2: 2 }; // Error

OneOf<A, B> (same as XOR)

OneOf will create exclusive set where you can set only one or other type

Usage:

  type exclusive = OneOf<{ property: string }, { property2: number }>;
 
  let result: exclusive;
  result = { property: 'test' }; // Ok
  result = { property2: 1 }; // Ok
  result = { property: 'test', property2: 2 }; // Error

Extend<A, B>

Extend will extend A type with B type

usage:

  type Extended = Extend<{ a: string }, { b: string }>;
 
  // Will result in
  type Extended = { a: string, b: string };

Subtract<A, B>

Subtract will subtract B type from A type

Usage:

  type Subtracted = Subtract<{ a: string, b: string }, { b: string }>;
 
  // Will result in
  type Subtracted = { a: string };

Nullable<A>

Nullable will make possible to assign null to variable.

Usage:

  let test: Nullable<string> = null;
  test = 'Will work';

RecordObject<A, B>

RecordObject creates type for object which each key has same type.

Usage:

  const mySizes = {
    big: '22px',
    medium: 14,
    small: '1rem',
  };
  type MySizes = RecordObject<keyof typeof mySizes, string | number>

ValuesOf<A>

ValuesOf Returns values of constant array as type.

Usage:

 const myArray = ['test', 'of', 'value', 'of'] as const;
 type AllowedValue = ValueOf<typeof myArray>; // 'test' | 'of' | 'values';

Install

npm i typescript-utility-types

DownloadsWeekly Downloads

12

Version

1.1.1

License

MIT

Unpacked Size

8.06 kB

Total Files

5

Last publish

Collaborators

  • avatar