3 ms·
It is "erased" in this example in the sense that the hashes are just strings at runtime, and not other objects instead. That's because the `generateHash` functi
by mattstir 2y ago
It is "erased" in this example in the sense that the hashes are just strings at runtime, and not other objects instead. That's because the `generateHash` function in the article's example uses `as Hash` to tell the compiler "yes, I know I'm only returning a string here, but trust me, this is actually a `string & { [__brand]: 'Hash' }`".
That `as Hash` is known as a type assertion in Typescript and is normally used when the developer knows something info about the code that Typescript can't.
- hombre_fatal 2y agoWhat's nice here is that `makeHash: Hash = (x: string) => x as Hash` checks that `x` actually overlaps with the branded type (string, in this case). At first it looks like you could just lie and say `x as User` or something but it's not the case.