Kit
Type aliases

NominalType

NominalType<TKey, TMarker> = { readonly [K in `__${TKey}:@solana/kit`]: TMarker }

Use this to produce a nominal type.

This can be intersected with other base types to produce custom branded types.

Type Parameters

Type ParameterDescription
TKey extends stringThe name of the nominal type. This distinguishes one nominal type from another.
TMarker extends stringThe type of the value the nominal type can take.

Example

type SweeteningSubstance = 'aspartame' | 'cane-sugar' | 'stevia';
type Sweetener<T extends SweeteningSubstance> = NominalType<'sweetener', T>;
 
// This function accepts sweetened foods, except those with aspartame.
declare function eat(food: string & Sweetener<Exclude<SweeteningSubstance, 'aspartame'>>): void;
 
const artificiallySweetenedDessert = 'ice-cream' as string & Sweetener<'aspartame'>;
eat(artificiallySweetenedDessert); // ERROR

On this page