Kit
Type aliases

Brand

Brand<T, TBrandName> = NominalType<"brand", TBrandName> & T

Use this to produce a new type that satisfies the original type, but not the other way around. That is to say, the branded type is acceptable wherever the original type is specified, but wherever the branded type is specified, the original type will be insufficient.

You can use this to create specialized instances of strings, numbers, objects, and more which you would like to assert are special in some way (eg. numbers that are non-negative, strings which represent the names of foods, objects that have passed validation).

Type Parameters

Type ParameterDescription
TThe base type to brand
TBrandName extends stringA string that identifies a particular brand. Branded types with identical names will satisfy each other so long as their base types satisfy each other. Branded types with different names will never satisfy each other.

Example

const unverifiedName = 'Alice';
const verifiedName = unverifiedName as Brand<'Alice', 'VerifiedName'>;
 
'Alice' satisfies Brand<string, 'VerifiedName'>; // ERROR
'Alice' satisfies Brand<'Alice', 'VerifiedName'>; // ERROR
unverifiedName satisfies Brand<string, 'VerifiedName'>; // ERROR
verifiedName satisfies Brand<'Bob', 'VerifiedName'>; // ERROR
verifiedName satisfies Brand<'Alice', 'VerifiedName'>; // OK
verifiedName satisfies Brand<string, 'VerifiedName'>; // OK

On this page