3 ms·
Strings might not be the best way of demonstrating nominal typing, since that's already something TypeScript can manage: https://www.typescriptlang.org/play/?#c
by c-hendricks 2y ago
Strings might not be the best way of demonstrating nominal typing, since that's already something TypeScript can manage: https://www.typescriptlang.org/play/?#code/C4TwDgpgBAEghgZwBZQLxQAZMUg+gEgG8FgAnASwDsBzAXwwCgGAzAV0oGNhyB7SqahEoRScYBHjIAFFTCtgALigkKNAJRRCDKDqgc+JKNmRLJKdFhwFCs+fSa6opCMFal+xpA1pN9lQ-oAtmBwzmZoUFKepjgANFC2ispkVNRqSgBGPDwANhBw-KgAfJraus6u7lBkrBAA3N4NDH6GrAgiAJKUcsARAETtHBUAImJwfQ0tvZ4RgsKi4mZSbZ3d8mp1UAD0W0Y4CQhQPMzV4NBmTDtQACpI5IcA7nwA5L1BYOR5AIQMVwCCpGorECQl6x1OkCgzxUqWeByglB4vUQCHI1EocAyeWqPCgIVEIPEpCOJ1AkOeZmeADpfltrmcoTCaHD7gikVAUWiMVjoMBcWToM9CE58gATPg5EBQADauFwGVElFFAF0lH0zBMoLRqVIAEwAZgALABWNTNAy9XCBMQcJAQQ7od6hCQ4ZbtUhdHrxTwbS67W73b08B4QABuIni70+9p+Uyg1uAtvtESdYVdnniKw9a2AvqAA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAEghgZwB...
Also, since all examples of branded / nominal types in TypeScript use `as` (I assume to get around the fact that the object you're returning isn't actually of the shape you're saying it is...), you should read up on the pitfalls of it:
https://timdeschryver.dev/blog/stop-misusing-typescript-type-assertions https://timdeschryver.dev/blog/stop-misusing-typescript-type...
https://www.reddit.com/r/typescript/comments/z8f7mf/are_there_common_bad_usages_of_as/#:~:text=The%20only%20situation%20where%20you,need%20a%20type%20guard%20instead https://www.reddit.com/r/typescript/comments/z8f7mf/are_ther...
https://web.archive.org/web/20230529162209/https://www.bytelimes.com/why-you-should-avoid-type-assertions-in-typescript/ https://web.archive.org/web/20230529162209/https://www.bytel...
- deleted 2y ago[deleted]
- mattstir 2y agoI'm legitimately confused about why so many people in this thread are showing off template literal types as if actual hash functions just prepend "hash_" onto strings and call it a day. There are a lot of different types of data that don't have a predictable shape that TLTs just don't help with at all. While the pitfalls of mindlessly slapping `as XYZ` on lines to make it compile certainly exist (when the type definition changes without the areas with `as` being updated, etc), I don't know if branded values are really the place where they pop up. You brand a primitive when you want to differentiate it from other primitives that otherwise have the same underlying type. In that scenario, you can't really change the definition of the underlying primitive, so you can't really run into issues there.