1.1.17 • Published 2 years ago

advanced-types v1.1.17

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

advanced-types

Advanced type for Typescript.

TypeScript required! npm install -g typescript

If you have the desire to add any particular type you can apply at this link, if possible it will be satisfied as soon as possible.

Installation

yarn add advanced-types

Type list

Array:

NameDescription
FixedArrayGet an array with specific length and type
ValuesArrayGet all values of readonly array
IndexOfArrayByValueGet all index of element of readonly with specific type
IndexOfGet all index of readonly array
ArrayToStringGet a string from an array of strings

Number:

NameDescription
EnumerateGet a range of numbers starting from 0
RangeGet a range of numbers

String:

NameDescription
JoinGet the union of two strings by a separator.
NestedKeysGet a string obtained from an object's key tree by following its complete path.
NestedPathKeysGet a string obtained from an object's key tree by following its complete and incomplete path
RestrictedCharsGet string with only allowed chars
CheckMaxLengthCheck if string respect max length
SnakeCaseToCamelCaseGet a string snake case in camel case

Object:

NameDescription
ValueOfGet values type of object
NullableGet object with all values with possible value null or undefined
FilterKeyObjectByTypesGet keys object with specific keys with the type of the desired value.

Usage

Array

FixedArray

import { FixedArray } from  "advanced-types";
const  array1: FixedArray<number, 4> = [1,2,3,4];     //OK
const  array2: FixedArray<number, 4> = [1,2,3,4,5];  // ERROR 
const  array3: FixedArray<number, 4> = [1,2,3,"4"];  // ERROR

ValuesArray

import { ValuesArray } from  "advanced-types";
const  array1: ValuesArray<[1,2,"2"]> = "2";     //OK
const  array2: ValuesArray<[1,2,"2"]> = 2;       //OK
const  array3: ValuesArray<[1,2,"2"]> = 3;       //ERROR, possible values: 1 | 2 | "2"

IndexOfArrayByValue

import { IndexOfArrayByValue } from  "advanced-types";
const  array1: IndexOfArrayByValue<[1,2,"3","4"], string> = 2; // OK
const  array1: IndexOfArrayByValue<[1,2,"3","4"], string> = 0; // ERROR, possible values: 2 | 3

IndexOf

import { IndexOf } from  "advanced-types";
const  array1: IndexOf<["1", 10, {name: "name"}]> = 0; // OK
const  array1: IndexOf<["1", 10, {name: "name"}]> = 3; // ERROR, possible values: 0 | 1 | 2

ArrayToString

import { ArrayToString } from  "advanced-types";
const  string = ["a", "r","r","a","y"] as  const;
const  array: ArrayToString<typeof  string> = "array"; // OK
const  array: ArrayToString<typeof  string> = "arr";   // ERROR

Number

Enumerate

import { Enumerate } from  "advanced-types";
const  number1: Enumerate<5> = 2; // OK
const  number2: Enumerate<5> = 7;   // ERROR, ERROR, possible values: 0 | 1 | 2 | 3 | 4 | 5

Range

import { Range } from  "advanced-types";
const  number1: Range<2, 5> = 2   // OK
const  number2: Range<2, 5> = 6   // ERROR, ERROR, possible values: 2 | 3 | 4 | 5

String

Join

import { Join } from  "advanced-types";
const  string1: Join<"GP" | "SN", "01" | "02", "-"> = "SN-02"; // OK
const  string2: Join<"GP" | "SN", "01" | "02", "_"> = "GP_01"; // OK
const  string3: Join<"GP" | "SN", "01" | "02", "-"> = "SN-03"; // ERROR, possible values: "GP-01" | "GP-02" | "SN-01" | "SN-02"

NestedKeys

import { NestedKeys } from  "advanced-types";
const  object = {example:  "", key: { nestedKey:  2}}
const  string1: NestedKeys<typeof  object> = "key/nestedKey";      // OK
const  string2: NestedKeys<typeof  object, "."> = "key.nestedKey"; // OK
const  string3: NestedKeys<typeof  object, "."> = "key".           // ERROR, possible values: "example" | "key.nestedKey"

NestedPathKeys

import { NestedPathKeys } from  "advanced-types";
const  object = {example:  "", key: { nestedKey:  2}}
const  string1: NestedKeys<typeof  object> = "key/nestedKey";      // OK
const  string2: NestedKeys<typeof  object, "."> = "key.nestedKey"; // OK
const  string3: NestedKeys<typeof  object, "."> = "key".           // OK
const  string4: NestedKeys<typeof  object, "."> = "nestedKey".     // ERROR, possible values: "example" | "key.nestedKey" | "key"

RestrictedChars

import { RestrictedChars, Lower, Upper } from  "advanced-types";
const  string1: RestrictedChars<"example", "e" | "x" | "a" | "m" | "p" | "l"> = "example"; // OK
const  string2: RestrictedChars<"example", Lower> = "example"; // OK, Lower is a type of all lowercase letters
const  string3: RestrictedChars<"example", Upper> = "example"; // ERROR, Upper is a type of all uppercase letters
const  string4: RestrictedChars<"example", "e" | "x" | "a" | "m" | "p"> = "example";       // ERROR

CheckMaxLength

import { CheckMaxLength } from  "advanced-types";
const  string1: CheckMaxLength<"example", 10> = "example"; // OK
const  string2: CheckMaxLength<"example", 4> = "example";  // ERROR, string too long

SnakeCaseToCamelCase

import { SnakeCaseToCamelCase } from  "advanced-types";
const  string1: SnakeCaseToCamelCase<"example_camel_case"> = "exampleCamelCase"; // OK
const  string2: SnakeCaseToCamelCase<"example_camel_case"> = "example_camelcase"; // ERROR

Object

ValueOf

import { ValueOf } from  "advanced-types";
type  IExample = {example: string}
const  value1: ValueOf<IExample, "example"> = "2"; //OK
const  value2: ValueOf<IExample, "example"> = 2;   // ERROR, type number is not assignable to string

Nullable

import { Nullable } from  "advanced-types";
type  IExample = {example: string, example2: number};
const  string1: Nullable<IExample> = {example:  undefined, example2:  null}; // OK

FilterKeyObjectByTypes

import { FilterKeyObjectByTypes } from  "advanced-types";
type  IExample = {example: string, example2: number}
const  string1: FilterKeyObjectByTypes<IExample, number> = "example2"; // OK
const  string2: FilterKeyObjectByTypes<IExample, number> = "example";  // ERROR ERROR, possible values: "example2" 
1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago