Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Alternatively for the case of id strings with known prefixes, a unique feature of TypeScript is you can use template string literal types:

https://www.kravchyk.com/adding-type-safety-to-object-ids-ty...



Note that the prefix was never intended to be looked at as the real problem. That's not a hash function, that's an example hash function because TFA couldn't be bothered to implement a proper one. They're not actually trying to solve the prefix problem.


This is why I always use `Math.random().toString(16)` for my examples :D People often get lost on the details, but they see `Math.random()` and they instantly get it's... well, just a random thing.


Isn't there a risk with this approach that you may receive input with a repeated prefix when there's a variable of type `string` and the prefix is prepended to satisfy the type checker without checking if the prefix already exists?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: