Created
April 7, 2022 11:44
-
-
Save shqld/6b6f2e44e586fbbd8ce361ce7629c776 to your computer and use it in GitHub Desktop.
Nominal Number types in TypeScript
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
class Flavoring<FlavorT> { | |
private _flavor?: FlavorT; | |
} | |
type Flavor<Base, Flavor> = Base & Flavoring<Flavor>; | |
type i32 = Flavor<number, 'i32'> | |
type i64 = Flavor<number, 'i64'>; | |
const a: i32 = 1 | |
const b: i64 = 2 | |
let c = a | |
c = b // ERROR: Type 'i64' is not assignable to type 'i32'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.typescriptlang.org/play?#code/MYGwhgzhAEBi4DcD2AnAlgOwOYB55mRQBUA+aAbwChoboAHdBMAFwFNoB9AM0VQH4AXHF7EA3JQC+lSswCeddvkI4AQpFYAaYQVRkAvNDUR2AMm2FMuJbvEz57NAGYATNAPWUODAFcAtgCNWFC0AcidnEJI7BWg0ADYAFjdzVC8-QODoMMTI22AkDAhmaDAhcOSARkp8wuL-MsTk52kQVmLgZLBpDoN-SiA