Created
October 31, 2025 09:28
-
-
Save TotooriaHyperion/7fbe71b1c5c42255b7fbe71196cffae5 to your computer and use it in GitHub Desktop.
Typescript Distinct String Type to avoid incorrect assign between different alias.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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