3 ms·
What he means is that you didn't _used_ to be able to do this: declare const NOMINAL_BRAND: unique symbol; enum UserIdType {} type UserId = string
by svieira 4y ago
What he means is that you didn't _used_ to be able to do this:
declare const NOMINAL_BRAND: unique symbol;
enum UserIdType {}
type UserId = string & {[NOMINAL_BRAND]: UserIdType}
type UserMap<V> = { [key: UserId]: V };
But at some point in the past few years, this is now supported and things like this work as expected:
const userId: UserId = "234" as UserId
const coolUsers: UserMap<boolean> = {}
coolUsers[userId] = true // works
coolUsers["isThisOkay"] = false // fails
- ajkjk 4y agoInteresting, I thought that wasn't possible, but now I feel like I'm just confused and mixing it up with something else that wasn't possible but can't remember well enough. But anyway, what I want from TS is type UserId = unique string; // or something const userId = "1234" as UserId and no further boilerplate or trickery to get it to work. Would certainly be nice.
- scotty79 4y agoYou can get close to that: https://github.com/seancroach/ts-opaque https://github.com/seancroach/ts-opaque