HexChar

Syntax

type HexChar

Represents a single character in the hexadecimal range (0-9, A-F/a-f).

Example

import { HexChar } from 'potence';

function colorHasComponent(color: string, component: HexChar): boolean {
    return color.toLowerCase().includes(component.toLowerCase());
}

colorHasComponent('#ff0000', 'F');  // true
colorHasComponent('#ff0000', '5');  // false
colorHasComponent('#ff0000', 'Z');  // error: 'Z' is not assignable to type 'HexChar'

Remarks

Though TypeScript supports template literal types since TypeScript 4.1, they can only represent simple expressions due to the fact that they essentially populate a list containing all possible string combinatoins. In the case of a type like

type HexColor = `${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}`

TypeScript will throw an error because the number of combinations is too large for TypeScript to safely handle.

As a result, until either lazily evaluated template literals or RegEx-validated string types are implemented, the usefulness of this type is rather limited.