The demo they posted demonstrates how to do it. But I don’t think it’s a generally good solution to the problem, it feels like it solves this specific case where the type is a string hash. I think the evolution of this for other types and objects is more like what the OP article suggests.
I wonder if a more natural solution would be to extend the String class and use that to wrap/guard things:
As mentioned elsewhere, what this is actually doing is showing that string and String are not structurally equivalent in TS.
If you add another class Email that extends String, you can pass it as a Hash without any problems. And you can get rid of the Hash stuff altogether and do something like
compareHash(userInput, new String(userInput));
and that fails just as well as the Hash example.
Using extends like this doesn't actually fix the problem for real.
I wonder if a more natural solution would be to extend the String class and use that to wrap/guard things:
class Hash extends String {}
compareHash(hash: Hash, input: string)
Here's an example: https://www.typescriptlang.org/play/?#code/MYGwhgzhAEASkAtoF...