@oxog/kindof
Version:
Zero-dependency advanced type detection library with TypeScript support, plugin system, and 100% test coverage
98 lines (88 loc) • 3.17 kB
TypeScript
declare function toString(value: unknown): string;
declare function toNumber(value: unknown): number | null;
declare function toBoolean(value: unknown): boolean;
declare function toBigInt(value: unknown): bigint | null;
declare function toSymbol(value: unknown): symbol;
declare function toArray(value: unknown): unknown[] | null;
declare function toObject(value: unknown): Record<string, unknown> | null;
declare function toMap(value: unknown): Map<unknown, unknown> | null;
declare function toSet(value: unknown): Set<unknown> | null;
declare function toDate(value: unknown): Date | null;
declare function toRegExp(value: unknown): RegExp | null;
declare function toError(value: unknown): Error | null;
declare function toFunction(value: unknown): ((...args: any[]) => any) | null;
declare function toPromise(value: unknown): Promise<unknown> | null;
declare function toBuffer(value: unknown): Buffer | null;
declare function toTypedArray<T extends ArrayBufferView>(value: unknown, TypedArrayConstructor: new (buffer: ArrayBuffer) => T): T | null;
interface TypeMap {
'undefined': undefined;
'null': null;
'boolean': boolean;
'number': number;
'string': string;
'symbol': symbol;
'bigint': bigint;
'object': object;
'array': any[];
'function': Function;
'date': Date;
'regexp': RegExp;
'error': Error;
'promise': Promise<any>;
'map': Map<any, any>;
'set': Set<any>;
'weakmap': WeakMap<any, any>;
'weakset': WeakSet<any>;
'int8array': Int8Array;
'uint8array': Uint8Array;
'uint8clampedarray': Uint8ClampedArray;
'int16array': Int16Array;
'uint16array': Uint16Array;
'int32array': Int32Array;
'uint32array': Uint32Array;
'float32array': Float32Array;
'float64array': Float64Array;
'bigint64array': BigInt64Array;
'biguint64array': BigUint64Array;
'generatorfunction': GeneratorFunction;
'asyncfunction': AsyncFunction;
'asyncgeneratorfunction': AsyncGeneratorFunction;
'proxy': any;
'dataview': DataView;
'arraybuffer': ArrayBuffer;
'sharedarraybuffer': SharedArrayBuffer;
'arguments': IArguments;
'buffer': Buffer;
'stream': NodeJS.ReadableStream | NodeJS.WritableStream;
'eventemitter': NodeJS.EventEmitter;
'element': Element;
'node': Node;
'window': Window;
'document': Document;
'global': typeof globalThis;
}
type TypeName = keyof TypeMap;
// Function type helpers
interface GeneratorFunction {
new(...args: any[]): Generator;
(...args: any[]): Generator;
}
interface AsyncFunction {
new(...args: any[]): Promise<any>;
(...args: any[]): Promise<any>;
}
interface AsyncGeneratorFunction {
new(...args: any[]): AsyncGenerator;
(...args: any[]): AsyncGenerator;
}
// Node.js type helpers (will be undefined in browser)
declare global {
namespace NodeJS {
interface ReadableStream {}
interface WritableStream {}
interface EventEmitter {}
}
interface Buffer {}
}
declare function coerceType<T extends TypeName>(value: unknown, targetType: T): TypeMap[T] | null;
export { coerceType, toArray, toBigInt, toBoolean, toBuffer, toDate, toError, toFunction, toMap, toNumber, toObject, toPromise, toRegExp, toSet, toString, toSymbol, toTypedArray };