tool-types
v0.1.1
Published
Complementing TypeScript built-in types
Downloads
3
Maintainers
Readme
tool-types
Complementing TypeScript built-in types.
Installation
npm install tool-types
# or yarn add tool-types
Usage
import { TupleUnion } from 'tool-types';
type T0 = [number, string];
type T1 = TupleUnion<T0> // number | string
Utility Types
Intersect
Except
UnionOmit
TupleUnion
RequireAtLeastOne
RequireAtLeastOneByKeys
Weaken
Promisify
DeepPartial
DeepRequired
DeepReadOnly
DeepMutable
RequiredByKeys
PartialByKeys
ReadOnlyByKeys
MutableByKeys
PickAllKeys
PickRequiredKeys
PickPartialKeys
PickRequired
PickPartial
Equal
PickReadOnlyKeys
PickReadOnly
PickMutableKeys
PickMutable
Deprecated API
Intersect
type A = {
name: string;
age: number;
};
type B = {
name: string;
address: string;
gender: number;
}
// Expect: { name: string; }
type ResultType = Intersect<A, B>;
Except
type A = {
name: string;
age: number;
};
type B = {
name: string;
address: string;
gender: number;
}
// Expect: { age: number; }
type ResultType = Except<A, B>;
UnionOmit
type A = {
value: string;
disabled?: boolean;
onChange: () => void;
};
type B = {
value: number;
onChange: () => void;
};
/* Expect:
{
value: string,
disabled?: boolean;
onChange: () => void;
}
*/
type ResultType = UnionOmit<A, B>;
TupleUnion
type A = ['a', 'b', 'c'];
// Expect: 'a' | 'b' | 'c'
type ResultType = TupleUnion<A>;
RequireAtLeastOne
type A = {
name: string;
age: number;
}
/* Expect:
| {name?: string; age: number;}
| {name: string; age?: number;}
*/
type ResultType = RequireAtLeastOne<A>;
RequireAtLeastOneByKeys
type A = {
name: string;
age: number;
gender: number;
}
/* Expect:
| {
name?: string;
age: number;
gender: number;
}
| {
name: string;
age: number;
gender?: number;
}
*/
type ResultType = RequireAtLeastOneByKeys<A, 'name' | 'gender'>;
Weaken
type A = {
name: string;
say(word: string): string;
}
type B = 'name'
// Expect: { name: any; say(word: string): string; }
type ResultType =Weaken<A, B>;
Promisify
const foo = (): Promise<string> => {
return new Promise(resolve => {
resolve('hello world');
});
};
// Expect: string
type ResultType = Promisify<typeof foo>;
DeepPartial
type A = {
value: string;
next: {
data: number;
}
}
/* Expect:
{
value?: string;
next?: {
data?: number;
}
}
*/
type ResultType = DeepPartial<A>;
DeepRequired
type A = {
value?: string;
next?: {
data?: number;
}
}
/* Expect:
{
value: string;
next: {
data: number;
}
}
*/
type ResultType = DeepRequired<A>;
DeepReadOnly
type A = {value: string; next: { data: number; }}
/* Expect:
{
readonly value: string;
readonly next: {
readonly data: number;
}
}
*/
type ResultType = DeepReadOnly<A>;
DeepMutable
type A = {
readonly value: string;
readonly next: {
readonly data: number;
}
}
/* Expect:
{
value: string;
next: {
data: number;
}
}
*/
type ResultType = DeepMutable<A>;
RequiredByKeys
type A = {
name?: string;
age?: number;
gender?: number;
}
/* Expect:
{
name: string;
age?: number;
gender?: number;
}
*/
type ResultType = RequiredByKeys<A, 'name'>;
PartialByKeys
type A = {
name: string;
age: number;
gender: number;
}
/* Expect:
{
name?: string;
age: number;
gender: number;
}
*/
type ResultType = PartialKeys<A, 'name'>;
ReadOnlyByKeys
type A = {
name: string;
age: number;
gender: number;
}
/* Expect:
{
readonly name: string;
age: number;
gender: number;
}
*/
type ResultType = ReadOnlyByKeys<A, 'name'>;
MutableByKeys
type A = {
readonly name: string;
readonly age: number;
readonly gender: number;
}
/* Expect:
{
name: string;
readonly age: number;
readonly gender: number;
}
*/
type ResultType = MutableByKeys<A, 'name'>;
PickAllKeys
type A = {
name: string;
age?: number;
gender?: number;
}
// Expect: name | age | gender
type ResultType = PickAllKeys<A>;
PickRequiredKeys
type A = {
name: string;
age?: number;
gender?: number;
}
// Expect: name
type ResultType = PickRequiredKeys<A>;
PickPartialKeys
type A = {
name: string;
age?: number;
gender?: number;
}
// Expect: age | gender
type ResultType = PickPartialKeys<A>;
PickRequired
type A = {
name: string;
age?: number;
gender?: number;
}
// Expect: { name: string }
type ResultType = PickRequired<A>;
PickPartial
type A = {
name: string;
age?: number;
gender?: number;
}
// Expect: { age?: number; gender?: number;}
type ResultType = PickPartial<A>;
Equal
type A = { name: string; }
type B = { name?: string; }
// Expect: false
type ResultType = Equal<A, B>;
PickReadOnlyKeys
type A = {
readonly name: string;
age: number;
}
// Expect: name
type ResultType = PickReadOnlyKeys<A>;
PickReadOnly
type A = {
readonly name: string;
age: number;
}
// Expect: { readonly name: string; }
type ResultType = PickReadOnly<A>;
PickMutableKeys
type A = {
readonly name: string;
age: number;
}
// Expect: age
type ResultType = PickMutableKeys<A>;
PickMutable
type A = {
readonly name: string;
age: number;
}
// Expect: { age: number; }
type ResultType = PickMutable<A>;