3.4.0 • Published 2 years ago
@voxpelli/type-helpers v3.4.0
@voxpelli/type-helpers
My personal type helpers
Usage
JSDoc / Types in JS
/** @typedef {import('@voxpelli/type-helpers').PartialKeys} PartialKeys */TypeScript
import type { PartialKeys } from '@voxpelli/type-helpers';Declaration types
A mechanism for third-party extendible discriminated unions.
AnyDeclaration<Declarations, [DeclarationsExtras={}]>– returns a union of all valid declarations inDeclarationsAnyDeclarationType<Declarations, [DeclarationsExtras={}]>– returns a union of the type names from all valid declarations inDeclarationsValidDeclaration<TypeName, Declarations, [DeclarationsExtras={}]>– the base type of a valid declaration forDeclarations/DeclarationsExtrasthat also validates thatTypeNameexists as a valid declaration inDeclarations
Details on how to use declarations
Valid declarations
- Are part of
Declarations - Complies with the
DeclarationsExtrastype - Has a
typekey that matches a declarations key inDeclarations
Declaration types example
Imaginary module @voxpelli/foo:
import type {
AnyDeclaration,
AnyDeclarationType,
ValidDeclaration
} from '@voxpelli/type-helpers';
interface FooDeclarationExtras {
value: unknown
}
export interface FooDeclarations {
bar: FooDeclaration<'bar', { abc: 123 }>,
// This is a recommended addition, ensuring consumers stay alert and are aware
// that extensions here can be of all kinds of types
unknown: FooDeclaration<'unknown', unknown>,
}
export type AnyFooDeclaration = AnyDeclaration<FooDeclarations, FooDeclarationExtras>;
export type AnyFooDeclarationType = AnyDeclarationType<FooDeclarations, FooDeclarationExtras>;
export interface FooDeclaration<TypeName extends AnyFooDeclarationType, Value>
extends ValidDeclaration<TypeName, FooDeclarations, FooDeclarationExtras>
{
value: Value
}Third party extension:
import type { FooDeclaration } from '@voxpelli/foo';
declare module '@voxpelli/foo' {
interface FooDeclarations {
xyz: FooDeclaration<'xyz', { xyz: true }>
}
}Usage as a :
/**
* @param {AnyFooDeclaration} foo
*/
function timeToFoo (foo) {
switch (foo.type) {
case 'bar':
// foo.value.abc will be know to exist and be a number
break;
case 'xyz':
// If the third party extension has been included, then foo.value.xyz will be know to exist here and be a boolean
break;
default:
// foo.value is eg. unknown here
}
}Object types
ObjectEntry<T>– a typed equivalent to all invidiual itemsObject.entries()returnsObjectEntries<T>– an array ofObjectEntry<T>, mmore similar to whatObject.entries()returnsObjectFromEntries<T>– a typed equivalent of whatObject.fromEntries()returnsPartialKeys<Foo, 'abc'>– makes the keyabcofFoooptionalUnknownObjectEntry– the least specific entry forObjectFromEntries<T>and whatTneeds to be a subset of there
String types
NonGenericString<T, [ErrorMessage]>– ensures thatTis not a genericstring(and as such likely a string literal)NonGenericStringArray<T, [ErrorMessage]>– similar toNonGenericStringbut withTbeing anArray/ReadonlyArray
Util types
Equal<A, B>– ifA extends B, then resolves toA, else resolved toneverLiteralTypeOf<T>– resolves to a string literal that matches thetypeofoperatorMaybePromised<T>– resolves toT | Promise<T>
Used by
@voxpelli/typed-utils– my personal (type-enabled) utils / helpersumzeption– my original project for the initial types of this module
Similar modules
type-fest– a large colelction of type helpers