Skip to content

Instantly share code, notes, and snippets.

@TotooriaHyperion
Created October 31, 2025 09:28
Show Gist options
  • Select an option

  • Save TotooriaHyperion/7fbe71b1c5c42255b7fbe71196cffae5 to your computer and use it in GitHub Desktop.

Select an option

Save TotooriaHyperion/7fbe71b1c5c42255b7fbe71196cffae5 to your computer and use it in GitHub Desktop.
Typescript Distinct String Type to avoid incorrect assign between different alias.
const InternalDistinctStringSymbol = Symbol('');
export type IDistinctString<T> = string & { [InternalDistinctStringSymbol]?: T };
export type IValue = IDistinctString<'value'>;
export type IAlias1 = IDistinctString<'alias1'>;
export type IAlias2 = IDistinctString<'alias2'>;
const a: IAlias1 = 'a'; // ok
// @ts-expect-error
const b: IAlias2 = a; // ts-error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment