@byndyusoft-ui/types
v0.1.1
Published
Byndyusoft UI Utility Types
Downloads
230
Readme
types
TODO: description
Usage
Import library
const types = require('@byndyusoft-ui/types');
or
import types from '@byndyusoft-ui/types';
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 = 'Яблоко' | 'Апельсин'
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
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';
};