4 ms·
I don't understand why users of branded types use strings as brands. Also using a utility type makes unreadable the TypeScript errors related to this type. If
by conaclos 2y ago
I don't understand why users of branded types use strings as brands. Also using a utility type makes unreadable the TypeScript errors related to this type.
If I had to use branded types, I personally would prefer a different approach:
declare const USER_BRAND: unique symbol
type User = { name: string, [USER_BRAND]: undefined }
This also allows subtyping:
declare const PERSON_BRAND: unique symbol
type Person = { name: string, age: number, [USER_BRAND]: undefined, [PERSON_BRAND]: undefined }
Although this is sometimes convenient, I always find branded types too clever. I would prefer a dedicated syntax for nominal types.
I made my own proposal: https://github.com/microsoft/TypeScript/issues/202#issuecomment-1412793902 https://github.com/microsoft/TypeScript/issues/202#issuecomm...
- AlienRobot 2y agoiirc this is why the hungarian notation existed. They had ints for columns and rows and they used a prefix on variable names to distinguish which ints were columns and which ints were rows. Though I may be misremembering.