0.1.1 • Published 3 years ago

tool-types v0.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

tool-types

Complementing TypeScript built-in types.

npm.io npm.io

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

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>;

⇧ back to top

Except

type A = { 
    name: string;
    age: number;
};
type B = { 
    name: string;
    address: string;
    gender: number;
}
// Expect: { age: number; }
type ResultType = Except<A, B>;

⇧ back to top

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>;

⇧ back to top

TupleUnion

type A = ['a', 'b', 'c'];
// Expect: 'a' | 'b' | 'c'
type ResultType = TupleUnion<A>;

⇧ back to top

RequireAtLeastOne

type A = {
    name: string;
    age: number;
}
/* Expect: 
| {name?: string; age: number;} 
| {name: string; age?: number;}
*/
type ResultType = RequireAtLeastOne<A>;

⇧ back to top

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'>;

⇧ back to top

Weaken

type A = {
    name: string; 
    say(word: string): string;
}
type B = 'name'
// Expect: { name: any; say(word: string): string; }
type ResultType =Weaken<A, B>;

⇧ back to top

Promisify

const foo = (): Promise<string> => {
  return new Promise(resolve => {
      resolve('hello world');
  });
};
// Expect: string
type ResultType = Promisify<typeof foo>;

⇧ back to top

DeepPartial

type A = { 
    value: string; 
    next: { 
        data: number;
    }
}
/* Expect: 
{ 
    value?: string; 
    next?: { 
        data?: number;
    }
}
*/
type ResultType = DeepPartial<A>;

⇧ back to top

DeepRequired

type A = { 
    value?: string;
    next?: { 
        data?: number;
    }
}
/* Expect: 
{ 
    value: string; 
    next: { 
        data: number;
    }
}
*/
type ResultType = DeepRequired<A>;

⇧ back to top

DeepReadOnly

type A = {value: string; next: { data: number; }}
/* Expect: 
{ 
    readonly value: string; 
    readonly next: { 
        readonly data: number; 
    }
}
*/
type ResultType = DeepReadOnly<A>;

⇧ back to top

DeepMutable

type A = { 
    readonly value: string; 
    readonly next: {
        readonly data: number;
    }
}
/* Expect: 
{ 
    value: string; 
    next: { 
        data: number;
    }
}
*/
type ResultType = DeepMutable<A>;

⇧ back to top

RequiredByKeys

type A = { 
    name?: string; 
    age?: number; 
    gender?: number;
}
/* Expect: 
{ 
    name: string; 
    age?: number; 
    gender?: number;
}
*/
type ResultType = RequiredByKeys<A, 'name'>;

⇧ back to top

PartialByKeys

type A = { 
    name: string; 
    age: number; 
    gender: number;
}
/* Expect: 
{ 
    name?: string; 
    age: number; 
    gender: number;
}
*/
type ResultType = PartialKeys<A, 'name'>;

⇧ back to top

ReadOnlyByKeys

type A = { 
    name: string; 
    age: number; 
    gender: number;
}
/* Expect: 
{ 
    readonly name: string; 
    age: number; 
    gender: number;
}
*/
type ResultType = ReadOnlyByKeys<A, 'name'>;

⇧ back to top

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'>;

⇧ back to top

PickAllKeys

type A = { 
    name: string; 
    age?: number; 
    gender?: number;
}
// Expect: name | age | gender
type ResultType = PickAllKeys<A>;

⇧ back to top

PickRequiredKeys

type A = { 
    name: string; 
    age?: number; 
    gender?: number;
}
// Expect: name
type ResultType = PickRequiredKeys<A>;

⇧ back to top

PickPartialKeys

type A = { 
    name: string; 
    age?: number; 
    gender?: number;
}
// Expect: age | gender
type ResultType = PickPartialKeys<A>;

⇧ back to top

PickRequired

type A = { 
    name: string; 
    age?: number; 
    gender?: number;
}
// Expect: { name: string }
type ResultType = PickRequired<A>;

⇧ back to top

PickPartial

type A = { 
    name: string; 
    age?: number; 
    gender?: number;
}
// Expect: { age?: number; gender?: number;}
type ResultType = PickPartial<A>;

⇧ back to top

Equal

type A = { name: string; }
type B = { name?: string; }
// Expect: false
type ResultType = Equal<A, B>;

⇧ back to top

PickReadOnlyKeys

type A = { 
    readonly name: string; 
    age: number; 
}
// Expect: name
type ResultType =  PickReadOnlyKeys<A>;

⇧ back to top

PickReadOnly

type A = { 
    readonly name: string;
    age: number;
}
// Expect: { readonly name: string; }
type ResultType = PickReadOnly<A>;

⇧ back to top

PickMutableKeys

type A = { 
    readonly name: string;
    age: number;
}
// Expect: age
type ResultType = PickMutableKeys<A>;

⇧ back to top

PickMutable

type A = { 
    readonly name: string;
    age: number;
}
// Expect: { age: number; }
type ResultType = PickMutable<A>;

⇧ back to top