@litert/utils-ts-types
v1.6.0
Published
The static type utilities for TypeScript.
Downloads
315
Maintainers
Readme
LiteRT/Utils - TypeScript Types
The utility functions/classes/constants about helper types for TypeScript.
Requirement
- TypeScript v5.0.0 (or newer)
- Node.js v18.0.0 (or newer)
Installation
npm i @litert/utils-ts-types --saveUsage
Type IJsonSafeValue
The IJsonSafeValue type is a TypeScript type that express a JSON safe value, which could be safely passed to
JSON.stringify, and may be returned by JSON.parse.
Thus, only the following types are allowed:
- Basic Types:
string,number,boolean,null - Array of JSON safe values
- Object with string keys and JSON safe values
Note that undefined, function, symbol, bigint and other non-JSON safe types are not allowed.
Type IObject
The IObject type is a type stands for any object values, usefully in generic constraints.
And this helps to avoid the ESLint rule
@typescript-eslint/ban-typeserror.
type ISomeType<T extends IObject> = ...;Type IDict
The IDict is a lite form of Record<string, any>, which is more suitable to express a dictionary object.
const dictOfAny: IDict = {
key1: 'value1',
key2: 42,
key3: true,
};
const dictOfString: IDict<string> = {
key1: 'value1',
key2: '42',
key3: 'true',
};Type IFunction and IAsyncFunction
The IFunction type is a type stands for any function values, usefully in generic constraints.
type ISomeType<T extends IFunction> = ...;Type IDeepPartial
The IDeepPartial<T> type is a recursive version of the built-in Partial<T> type.
It makes all properties of an object type T optional, including nested properties.
Type IConstructor
The IConstructor type is a type that represents a class constructor function.
class MyClass {};
const ctor: IConstructor<MyClass> = MyClass;Type IElementOfArray
The IElementOfArray<T> type is a utility type that extracts the element type from an array type T.
type MyArray = string[];
const arr: MyArray = ['a', 'b', 'c'];
const el0: IElementOfArray<MyArray> = arr[0];Type IMaybeArray
The IMaybeArray<T> type is a utility type that represents a value that can be either a single value of type T or an array of type T.
This type is useful for function parameters or some options that can accept either a single value or multiple values.
interface IOptions {
tag: IMaybeArray<string>;
}
function processItems(items: IMaybeArray<string>) {
const itemArray = Array.isArray(items) ? items : [items];
// ...
}Type IMaybeAsync
The IMaybeAsync<T> type is a utility type that represents a value that can be either of type T or a Promise that resolves to type T.
This type is useful for the return type of functions that can be either synchronous or asynchronous.
function maybeAsync(flag: boolean): IMaybeAsync<string> {
if (flag) {
return 'synchronous value';
} else {
return Promise.resolve('asynchronous value');
}
}Type IfIsAny
The IfIsAny<T, TYes, TNo> type is a conditional type that checks if the type T is any.
If T is any, it resolves to TYes; otherwise, it resolves to TNo.
Type IfIsNever
The IfIsNever<T, TYes, TNo> type is a conditional type that checks if the type T is never.
If T is never, it resolves to TYes; otherwise, it resolves to TNo.
Documentation
License
This library is published under Apache-2.0 license.
