|
|
|
|
|
by Vanit
1935 days ago
|
|
It's kind of amusing reading this as a Typescript dev because I'm so used to Typescript's expressiveness I couldn't really work out which trick it was trying to tell me was novel. The answer is structural typing (aka duck typing). |
|
In a language with structural typing, two types are considered equivalent if they share the same structure. With phantom types we can have a single structure and disambiguate between different uses without touching the underling structure. There's no difference between Id(User) and Id(Post) at runtime, the annotation is purely a compile-time restraint.
In fact, because of typescript's structural typing, the only way to use phantom types in ts is to have some dummy field of type `never` that the type system can use to disambiguate between the two [1].
[1]: https://gist.github.com/GoNZooo/243b23702df1fae38c966ae18832...