/* eslint-disable id-length, max-classes-per-file */ import {describe, it} from 'node:test'; import { AnyFunction, KeysWithValuesOfType, Constructor, AnyArgs, ExtractKeys, SubSet, PartialPick, TupleExtract, ParamsExtract, RemoveNever } from '../types/utils'; import {assertType} from '../types/assertType'; describe('utils', () => { describe('ExtractKeys', () => { it('should extract the keys of an object that match the keys of another object', () => { const obj = { a: 1, b: '2', c: 3, d: () => {}, e: '4', f: {}, g: () => {} }; assertType>('a'); assertType>('b'); // @ts-expect-error - 'm' is not a key of obj assertType>('m'); }); }); describe('SubSet', () => { it('should return a subset of the passed object', () => { const obj = { a: 1, b: '2', c: 3, d: () => {}, e: '4', f: {}, g: () => {} }; assertType>({a: 1, b: '2'}); assertType>({a: 1, b: '2', c: 3}); // @ts-expect-error - 'm' is not a key of obj assertType>({a: 1, b: '2', m: '3'}); }); it('should work with complex objects', () => { const obj = { a: { b: 1, c: '2', d: { e: 1, f: '2' } } }; type match = { a: { b: number; d: { e: number; }; }; h: string; }; assertType>({ a: {b: 1, d: {e: 1}} }); assertType>({ h: '1' }); // @ts-expect-error - 'm' is not a key of obj assertType>({a: {b: 1, d: {e: 1}}, m: '2'}); // @ts-expect-error - 'm' is not a key of obj assertType>({a: {b: 1, d: {e: 1, m: '2'}}}); }); it('should work with index signatures', () => { const obj = { a: 1, b: '2' }; type match = { [key: string]: number; }; assertType>({a: 1}); // @ts-expect-error - 'm' is not a key of obj assertType>({a: 1, m: 3}); }); }); describe('Constructor', () => { it('should match any constructor', () => { class TestClass { a: number; b: string; constructor(a: number, b: string) { this.a = a; this.b = b; } } assertType>(TestClass); assertType>(TestClass); }); }); describe('AnyFunction', () => { it('should match any function', () => { assertType(() => {}); assertType(function () {}); assertType(function named() {}); assertType(async () => {}); assertType(async function () {}); assertType(async function named() {}); assertType(function* () {}); assertType(function* named() {}); }); }); describe('AnyArgs', () => { it('should match any array', () => { assertType([]); assertType([1, '2', {}]); }); }); describe('KeysWithValuesOfType', () => { it('should return the keys of an object with values of the specified type', () => { const obj = { a: 1, b: '2', c: 3, d: () => {}, e: '4', f: {}, g: () => {} }; assertType[]>(['b', 'e']); assertType[]>(['a', 'c']); assertType[]>(['d', 'g']); assertType[]>(['d', 'g']); assertType[]>(['f']); // @ts-expect-error - 'm' is not a key of obj assertType[]>(['b', 'e', 'm']); }); it('should also work with generic classes', () => { class TestClass { a: number; b: string; c: number; d: AnyFunction; e: string; f: object; g: AnyFunction; // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor() { this.a = 1; this.b = '2'; this.c = 3; this.d = () => {}; this.e = '4'; this.f = {}; this.g = () => {}; } } const instance = new TestClass(); assertType[]>(['b', 'e']); assertType[]>(['a', 'c']); assertType[]>(['d', 'g']); assertType[]>(['d', 'g']); assertType[]>(['f']); }); }); describe('PartialPick', () => { it('should return a new Type where all the keys of T are partial except the specified keys K union type', () => { const obj = { a: 1, b: '2', c: 3 }; assertType>({a: 1, b: '2'}); assertType>({a: 1, b: '2', c: 3}); // @ts-expect-error - 'a' and 'b' are not partial assertType>({c: 3}); }); }); describe('TupleExtract', () => { it('should return a new Type that extracts from the beginning of a Tuple of type TUPLE all the elements that match the specified type EXTRACTED in the beginning', () => { assertType>([3, 4]); assertType>([4]); assertType>([]); // @ts-expect-error - match tuple can not be extracted from the beginning of the first tuple assertType>([]); assertType>([]); }); }); describe('ParamsExtract', () => { it('should return a new Type that returns the params from the passed function F extracting from the params tuple those that match EXTRACTED in the beginning', () => { const fn = (a: number, b: string, c: boolean) => {}; type fnType = typeof fn; assertType>(['b', true]); // @ts-expect-error - 'c' is not a boolean not a number assertType>([1]); // @ts-expect-error - match tuple can not be extracted from the beginning of the params tuple assertType>([]); // @ts-expect-error - first param is not a boolean assertType>([1, '2']); }); it('should work with functions of only one param', () => { const fn = (a: number) => {}; type fnType = typeof fn; assertType>([]); }); }); describe('RemoveNever', () => { it('should remove never values from an object signature', () => { type obj = { a: 1; b: 2; c: never; d: never; }; assertType>({a: 1, b: 2}); // @ts-expect-error - 'c' is not a key of the object after removing never values assertType>({a: 1, b: 2, c: 3}); }); }); });