←back to thread

Parse, don't validate (2019)

(lexi-lambda.github.io)
398 points declanhaigh | 2 comments | | HN request time: 0.478s | source
1. conaclos ◴[] No.35054228[source]
While theoretically I accepted with all the points, in practice it is sometimes too bloated to add so many types that are barely distinct. It is sometimes better to trade safety for simplicity.

The trade-off is always hard to make. For instance: should I introduce a branded type for unsigned 32bit integer in TypeScript?

   type u32 = number & { [U32_BRAND]: never }
   const u32 = (n: number): u32 => (n >>> 0) as u32
And then to make it hard any use of this type:

   declare let x, y: u32
   y = u32(x + 1)
replies(1): >>35054364 #
2. Hackbraten ◴[] No.35054364[source]
I just learned that there’s an open issue [0], apparently for introducing a similar feature.

[0]: https://github.com/microsoft/TypeScript/issues/43505