My personal type helpers
npm install @voxpelli/type-helpersMy personal type helpers



``typescript`
/* @typedef {import('@voxpelli/type-helpers').PartialKeys} PartialKeys /
`typescript`
import type { PartialKeys } from '@voxpelli/type-helpers';
A mechanism for third-party extendible discriminated unions.
* AnyDeclaration – returns a union of all valid declarations in DeclarationsAnyDeclarationType
* – returns a union of the type names from all valid declarations in DeclarationsValidDeclaration
* – the base type of a valid declaration for Declarations / DeclarationsExtras that also validates that TypeName exists as a valid declaration in Declarations
#### Valid declarations
1. Are part of DeclarationsDeclarationsExtras
2. Complies with the typetype
3. Has a key that matches a declarations key in Declarations
#### Declaration types example
Imaginary module @voxpelli/foo:
`typescript
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
export type AnyFooDeclarationType = AnyDeclarationType
export interface FooDeclaration
extends ValidDeclaration
{
value: Value
}
`
Third party extension:
`typescript
import type { FooDeclaration } from '@voxpelli/foo';
declare module '@voxpelli/foo' {
interface FooDeclarations {
xyz: FooDeclaration<'xyz', { xyz: true }>
}
}
`
Usage as a :
`javascript`
/**
* @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 – a typed equivalent to all invidiual items Object.entries() returnsObjectEntries
* – an array of ObjectEntry, mmore similar to what Object.entries() returnsObjectFromEntries
* – a typed equivalent of what Object.fromEntries() returnsPartialKeys
* – makes the key abc of Foo optionalUnknownObjectEntry
* – the least specific entry for ObjectFromEntries and what T needs to be a subset of there
* NonGenericString – ensures that T is not a generic string (and as such likely a string literal)NonGenericStringArray
* – similar to NonGenericString but with T being an Array / ReadonlyArray
* Equal – if A extends B, then resolves to A, else resolved to neverLiteralTypeOf
* – resolves to a string literal that matches the typeof
* MaybePromised – resolves to T | Promise
* @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