3 ms·
Depending on what you're looking to do, you can use a tuple type to get an array of a certain length. [number, number, number]
by ahuth 5y ago
Depending on what you're looking to do, you can use a tuple type to get an array of a certain length.
[number, number, number]
- strogonoff 5y agoI meant length that depends on another type, not length known beforehand (e.g., if N widgets are defined somewhere, [up to] N elements can exist somewhere else, or configuration should not compile). Naturally, this had to involve generics (similar to [0], except I don’t recall why but the `length` property workaround didn’t work or didn’t provide sufficient type safety in my use case). Eventually, after reading workarounds in a related issue[1], I gave up. TypeScript’s type system is great for its intended use, but when applied to expressing schema constraints it quickly falls short and runtime code execution is required to validate a given structure with all the corresponding drawbacks. [0] https://stackoverflow.com/q/56034226/247441 https://stackoverflow.com/q/56034226/247441 [1] https://github.com/microsoft/TypeScript/issues/26223 https://github.com/microsoft/TypeScript/issues/26223
- jonny_eh 5y ago> not length known beforehand Is that a feature of other languages?
- strogonoff 5y agoHaven’t had much exposure to other statically typed languages recently, so can’t say off the top of my head. Intuitively it looks like something modern generics should allow easily though, doesn’t it? As to Dhall, for example, I could validate such a constraint (and more) using a function I can be reasonably sure is safe and won’t halt (i.e. without all the drawbacks of TS and other general-purpose languages).