type-plus
Version:
Provides additional types for TypeScript.
31 lines • 1.07 kB
TypeScript
import type { Widen } from '../utils/index.js';
import { typeSym, valueSym } from './constants.js';
/**
* Create a "flavored" version of a type.
* TypeScript will disallow mixing flavors,
* but will allow unflavored values of that type to be passed in where a flavored version is expected.
* This is a less restrictive form of branding.
*/
export type Flavor<F extends string, T> = [T] extends [null] | [undefined] | [symbol] | [void] ? FlavoredUnit<F, T> : Flavored<F> & T;
/**
* A flavored type of `F`
*/
export interface Flavored<F extends string> {
[typeSym]?: F;
}
/**
* A special flavored type for special types.
*/
export interface FlavoredUnit<F extends string, T> {
[typeSym]?: F;
[valueSym]: T;
}
/**
* Creates a brand creator with the specified type.
*/
export declare function flavor<F extends string>(type: F): <T>(subject: T) => Flavor<F, Widen<T>>;
/**
* Creates a branded value of specified type.
*/
export declare function flavor<F extends string, T>(type: F, subject: T): Flavor<F, Widen<T>>;
//# sourceMappingURL=flavor.d.ts.map