My personal type helpers
JSDoc / Types in JS
/** @typedef {import('@voxpelli/type-helpers').PartialKeys} PartialKeys */
import type { PartialKeys } from '@voxpelli/type-helpers';
A mechanism for third-party extendible discriminated unions.
-
AnyDeclaration<Declarations, [DeclarationsExtras={}]>
– returns a union of all valid declarations inDeclarations
-
AnyDeclarationType<Declarations, [DeclarationsExtras={}]>
– returns a union of the type names from all valid declarations inDeclarations
-
ValidDeclaration<TypeName, Declarations, [DeclarationsExtras={}]>
– the base type of a valid declaration forDeclarations
/DeclarationsExtras
that also validates thatTypeName
exists as a valid declaration inDeclarations
- Are part of
Declarations
- Complies with the
DeclarationsExtras
type - Has a
type
key that matches a declarations key inDeclarations
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
}
}
-
ObjectEntry<T>
– a typed equivalent to all invidiual itemsObject.entries()
returns -
ObjectEntries<T>
– an array ofObjectEntry<T>
, mmore similar to whatObject.entries()
returns -
ObjectFromEntries<T>
– a typed equivalent of whatObject.fromEntries()
returns -
PartialKeys<Foo, 'abc'>
– makes the keyabc
ofFoo
optional -
UnknownObjectEntry
– the least specific entry forObjectFromEntries<T>
and whatT
needs to be a subset of there
-
NonGenericString<T, [ErrorMessage]>
– ensures thatT
is not a genericstring
(and as such likely a string literal) -
NonGenericStringArray<T, [ErrorMessage]>
– similar toNonGenericString
but withT
being anArray
/ReadonlyArray
-
Equal<A, B>
– ifA extends B
, then resolves toA
, else resolved tonever
-
LiteralTypeOf<T>
– resolves to a string literal that matches thetypeof
operator -
MaybePromised<T>
– resolves toT | Promise<T>
-
@voxpelli/typed-utils
– my personal (type-enabled) utils / helpers -
umzeption
– my original project for the initial types of this module
-
type-fest
– a large colelction of type helpers