0.0.41 • Published 2 years ago

ts-generic-magic v0.0.41

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

ts-generic-magic

Licence

Description

I found that I need some complex generic type in my daily work, and I make some helpful utils in my project, and I package it to npm to keep updating for a long time

Installation

npm install -D ts-generic-magic
# yarn add -D ts-generic-magic
# pnpm install -D ts-generic-magic

Major utils

RecordType

Create record utils with the same type by your own

examples

type RecordDate<T> = RecordType<T, Date>

RecordDate<'created_at' | 'last_modified' | 'updated_at'>

Built-in record utils by RecordType

RecordString & RecordNumber

Background: Type string & number is the most appear in project

Record string and number, and deal with optional operations by string, just add optional flag in individual string It can simplify code and more readable

  e.g:
     type Test = RecordString<'A?' | 'B[]' | 'C[]?', 'D'> & RecordNumber<'E?' | 'F'>
     const test: Test = {
       A: '',       // optional
       C: [''],     // optional, and it is an array
       B: ['', ''], // required, and it is an array
       E: '',       // required
       F: ''        // required
     }

EitherOr

Either choose A or choose B and combine with the rest

// Correctly
const test: EitherOr<
  { A: string; B: string; C: string; D: string },
  'C' | 'D',
  'B'
> = {
  A: '',
  C: '',
  D: '',
};

// Correctly
const test: EitherOr<
  { A: string; B: string; C: string; D: string },
  'C' | 'D',
  'B'
> = {
  A: '',
  B: ''
};

// Failed
const test: EitherOr<
  { A: string; B: string; C: string; D: string },
  'C' | 'D',
  'B'
> = {
  A: '',
  B: '',
  C: '',  // failed
  D: ''   // failed
};

// Failed
const test: EitherOr<
  { A: string; B: string; C: string; D: string },
  'C' | 'D',
  'B'
> = {
  A: '',
  C: '',
  D: '',
  B: '' // failed
};

PickByType

Pick Object from type

     type Result = PickByType<{A: string; B?: string; C: number}, string>

     Result => {
         A: string;
         B?: string;
     }

Minor utils

ExcludeRequired

Can be help to filter required item out

type Object = {
    name: string;
    gender: 'male' | 'female';
    address?: string;
}

ExcludeRequired<Object>

result: {
    address?: string
}

ExcludeOptional

Can be help to filter Optional item out

type Object = {
    name: string;
    gender: 'male' | 'female';
    address?: string;
}

ExcludeOptional<Object>

result: {
    name: string;
    gender: 'male' | 'female'
}

PickToNever

Pick the key which you want to be never

  PickToNever<{A: string; B: string; C: string}, 'A' | 'C'>

  result: { A?: never; C?: never }

Notes

Would be add testing later

0.0.41

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago