@voxpelli/type-helpers
v3.4.0
Published
My personal type helpers
Downloads
10,489
Readme
@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 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
Details on how to use declarations
Valid declarations
- Are part of
Declarations
- Complies with the
DeclarationsExtras
type - Has a
type
key 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 keyabc
ofFoo
optionalUnknownObjectEntry
– the least specific entry forObjectFromEntries<T>
and whatT
needs to be a subset of there
String types
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
Util types
Equal<A, B>
– ifA extends B
, then resolves toA
, else resolved tonever
LiteralTypeOf<T>
– resolves to a string literal that matches thetypeof
operatorMaybePromised<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