1.1.1 • Published 4 years ago
typescript-utility-types v1.1.1
typescript-utility-types
Collection of generic types for TypeScript, complementing built-in mapped types and aliases.
Installation
- Install package
npm install --save-dev typescript-utility-types
- 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';
1.1.1
4 years ago
1.1.0
4 years ago
1.0.0
4 years ago
0.3.0
5 years ago
0.2.0
5 years ago
0.1.0
5 years ago
0.0.1-canary.2
5 years ago
0.0.1-canary.1
5 years ago
0.0.1-canary.0
5 years ago