@dulysse1/ts-helper
v1.0.0
Published
Typescript library for typing helpers ✨
Downloads
9
Maintainers
Readme
🛠 ts-helper 🛠
Typescript library for type helpers ✨
Getting Started 🆙
Prerequisites
Install Typescript
on your project
npm install typescript --save-dev
Or
yarn add typescript --dev
Or
pnpm i -D typescript
For best results, add this to your tsconfig.json
{
"compilerOptions": {
"strictNullChecks": true, // highly recommended (required by few utilities)
"strict": true, // this is optional, but enable whenever possible
"lib": ["es2015"] // this is the lowest supported standard library
}
}
How to use ? 🤔
With EcmaScript module ✅
import type { Num, Arr, Str } from "@dulysse1/ts-helper";
// now you can create your types!
Documentation 🧗
Here some examples:
👉 Numbers
Check if a number is positive
import type { Num } from "@dulysse1/ts-helper";
type A = Num.IsPositive<-2343>; // false
type B = Num.IsPositive<134>; // true
type C = Num.IsPositive<0>; // true
Add two numbers
import type { Num } from "@dulysse1/ts-helper";
type A = Num.Add<10, 10>; // 20
type B = Num.Add<-10, 10>; // 0
type C = Num.Add<-23, -34>; // -57
Multiply two numbers
import type { Num } from "@dulysse1/ts-helper";
type A = Num.Multiply<10, 10>; // 100
type B = Num.Multiply<-6, 7>; // -42
type C = Num.Multiply<234, 783>; // number
Divide two numbers
import type { Num } from "@dulysse1/ts-helper";
type A = Num.Divide<20, 10>; // 2
type B = Num.Divide<0, 7>; // 0
type C = Num.Divide<7, 0>; // number
👉 Object
Get keys of object by an optional filter
import type { Obj } from "@dulysse1/ts-helper";
type A = Obj.KeyOf<{ a: string; b: number }, string>; // "a"
Merge two type objects
import type { Obj } from "@dulysse1/ts-helper";
type A = type A = Obj.Merge<
{ a: string; },
{ b: number; }
>; // { a: string; b: number; }
👉 String
Split a string to array
import type { Str } from "@dulysse1/ts-helper";
type A = Str.Split<"coucou">; // ["c", "o", "u", "c", "o", "u"]
type B = Str.Split<"coucou", "c">; // ["ou", "ou"]
Replace all iteration of one character
import type { Str } from "@dulysse1/ts-helper";
type A = Str.ReplaceAll<"coucou", "c", "x">; // "xouxou"
👉 Array
Check if array is a tuple
import type { Arr } from "@dulysse1/ts-helper";
type A = Arr.IsTuple<number[]>; // false
type B = Arr.IsTuple<[1, 2, 3]>; // true
Reverse an array
import type { Arr } from "@dulysse1/ts-helper";
type A = Arr.Reverse<[1, 2, 3]>; // [3, 2, 1]
And many more besides! 😲
Do you have any ideas or recommendations for improvement? 🤔
Contact me! 😃
Author: Ulysse Dupont
Contact: [email protected]