0.3.0 • Published 8 months ago

@byndyusoft-ui/types v0.3.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

@byndyusoft-ui/types

Installation

npm i @byndyusoft-ui/types
# or
yarn add @byndyusoft-ui/types

Import library

import types from '@byndyusoft-ui/types';

Callback

Import:

import { Callback } from '@byndyusoft-ui/types';

Usage examples:

const callback: Callback = () => {};

callback(); // return void
const callback: Callback<never, number> = () => {
    return 123;
};

callback(); // return number
type TArg = string;

const callback: Callback<TArg> = str => {};

callback('some text'); // return void
type TArg = number;
type TReturn = string;

const callback: Callback<TArg, TReturn> = num => num.toString();

callback(100); // return string
type TArgs = [number];
type TReturn = string;

const callback: Callback<TArgs, TReturn> = num => num.toString();

callback(100); // return string
type TArgs = [number, number, number];
type TReturn = boolean;

const callback: Callback<TArgs, TReturn> = (num1, num2, num3) => {
    return 0 < num1 + num2 + num3;
};

callback(1, 2, 3); // return boolean
type TArgs = [Array<number>, { value: number }];
type TReturn = Array<string>;

const callback: Callback<TArgs, TReturn> = (numbers, obj) => {
    return numbers.map(num => String(num * obj.value));
};

callback([1, 2, 3, 4], { value: 100 }); // return Array<string>
type TArgs = [Array<number>, Array<string>];
type TReturn = string;

const callback: Callback<TArgs, TReturn> = (numbers, strings) => {
    const result = 'Any string';

    // ...some operations

    return result;
};

callback([1, 2, 3, 4], ['a', 'b']); // return string

Required arguments count

Requires between 0 and 2 type arguments:

Bad:
const callback: Callback<number, number, number> = (a, b, c) => {};
Right:

0 arguments:

const callback: Callback = () => {};

1 argument:

const callback: Callback<number> = (a) => {};
const callback: Callback<[number, number, number]> = (a, b, c) => {};

2 arguments:

const callback: Callback<never, string> = () => {
    return 'Text';
};
const callback: Callback<number, string> = (a) => {
    return 'Text';
};
const callback: Callback<[number, number, number], string> = (a, b, c) => {
    return 'Text';
};

EmptyObject

const str: {} = 'str'; // ok
const num: {} = 123; // ok
const bool: {} = true; // ok
import { EmptyObject } from '@byndyusoft-ui/types';

const str: EmptyObject = 'str'; // Type 'string' is not assignable to type 'EmptyObject'.
const num: EmptyObject = 123; // Type 'number' is not assignable to type 'EmptyObject'.
const bool: EmptyObject = true; // Type 'boolean' is not assignable to type 'EmptyObject'.

IsTuple

import { IsTuple } from '@byndyusoft-ui/types';

type Test1 = IsTuple<string>; // false
type Test2 = IsTuple<Array<string>>; // false
type Test3 = IsTuple<[string]>; // true
type Test4 = IsTuple<[string, number]>; // true

PlainObject

const date: object = new Date(); // ok
const rgx: object = new RegExp('[a-z]'); // ok 
import { PlainObject } from '@byndyusoft-ui/types';

const date: PlainObject = new Date(); // Type 'Date' is not assignable to type 'PlainObject<unknown>'.
const rgx: PlainObject = new RegExp('[a-z]'); // Type 'RegExp' is not assignable to type 'PlainObject<unknown>'. 

const a: PlainObject = { a: 'b', c: 'd' }; // ok

ValueOf

import { ValueOf } from '@byndyusoft-ui/types';

enum Fruits {
    APPLE = 'apple',
    ORANGE = 'orange'
}

const fruitsDictionary = {
    [Fruits.APPLE]: 'Яблоко' as string,
    [Fruits.ORANGE]: 'Апельсин' as string
};

const TFruitsDictionaryValues = ValueOf<typeof fruitsDictionary>;
// TFruitsDictionaryValues = 'Яблоко' | 'Апельсин'

TimeoutId

import { TimeoutId } from '@byndyusoft-ui/types';

const timeoutIdRef = useRef<TimeoutId | null>(null);

timeoutIdRef.current = setTimeout(() => {}, 1000);

clearTimeout(timeoutIdRef.current);
timeoutIdRef.current = null;

IntervalId

import { IntervalId } from '@byndyusoft-ui/types';

const intervalIdRef = useRef<IntervalId | null>(null);

intervalIdRef.current = setInterval(() => {}, 1000);

clearInterval(intervalIdRef.current);
intervalIdRef.current = null;