Re:Rewriting TypeScript in Rust? You'D have to be
by ajkjk on Wednesday December 14, 2022 @03:13PM
(#33989687)
I'm aware of some of those, but I think they're all suboptimal compared to actual nominal types.
In particular iirc you can't use your nominal/branded type as a map key in a type-safe way.
Re:Rewriting TypeScript in Rust? You'D have to be
by scotty79 on Thursday December 15, 2022 @04:39AM
(#33997230)
Not sure what you mean:
enum A {}
type B = { a: number } & A;
function newB(i:number) {
return { a: i } as B;
}
let b:B = newB(5);
let m:Map<B, string> = new Map();
m.set(b, "test");
m.set({ a: 2 }, "test"); // errors
Re:Rewriting TypeScript in Rust? You'D have to be
by svieira on Thursday December 15, 2022 @11:48AM
(#34002223)
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
Re:Rewriting TypeScript in Rust? You'D have to be
by ajkjk on Thursday December 15, 2022 @12:40PM
(#34002966)
Interesting, 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.
1. Use an enum
2. Use an intersection with an enum and literally anything else (except a number)
3. Use a private field (only works with classes)
4. Use a `declare const FOO: unique symbol` keyed object intersected with anything else (works with everything including numbers, lots of boilerplate to declare a new nominal type).