@papb/assorted-ts-utils
Personal collection of assorted TypeScript utility types
Highlights
StrictEqual<A, B>
IfAny<T, ThenType, ElseType>
IfUnknown<T, ThenType, ElseType>
IfNever<T, ThenType, ElseType>
ParseArray<A, B>
SetKeyType<BaseType, Key, NewType, AsOptional>
ReplaceKeyType<BaseType, Key, NewType, AsOptional>
Install
$ npm install @papb/assorted-ts-utils
Usage
import {
StrictEqual,
ReplaceKeyType,
ParseArray,
ReplaceAllTypeOccurrences,
IfAny,
IfNever
} from '@papb/assorted-ts-utils';
import {
tsAssertTrue,
tsAssertTypeAcceptsValue,
tsAssertTypesExactlyEqual,
tsAssertExtends
} from '@papb/assorted-ts-utils/assert';
type T1 = StrictEqual<{ a: 1 }, { a: 1 }>;
type T2 = StrictEqual<{ a: 1 }, { a: 1; b: undefined }>;
type T3 = ReplaceKeyType<
{ foo: string; bar: number },
'bar',
Date,
true
>;
type T4 = StrictEqual<
ParseArray<readonly [string, Date, number, null, ...boolean[]]>,
{
isReadonly: true;
first: string;
last: boolean;
minLength: 4;
maxLength: undefined;
tuplePrefix: readonly [string, Date, number, null];
arrayTail: readonly boolean[];
}
>;
tsAssertTrue<T4>();
tsAssertTrue<T2>();
type T5 = ReplaceAllTypeOccurrences<
{ foo: string; bar: () => string },
string,
number
>;
type T6 = IfAny<any, 'hello', 'world'>;
type T7 = IfAny<unknown, 'hello', 'world'>;
type T8 = IfNever<1 & 2, 'hello', 'world'>;
type T9 = { a: string; b: string | number };
const someValue =
tsAssertTypeAcceptsValue()<T9>()(
{ a: 'hello', b: 123 } as const
);
tsAssertTypesExactlyEqual()<typeof someValue>()<{ a: 'hello'; b: 123 }>();
tsAssertExtends()<typeof someValue>()<T9>();
Documentation
TODO
License
MIT © Pedro Augusto de Paula Barbosa