3 ms·
For what it’s worth, the typescript type system is at or nearly at a point where these kinds of constraints are possible (with the caveat that runtime validatio
by eyelidlessness 6y ago
For what it’s worth, the typescript type system is at or nearly at a point where these kinds of constraints are possible (with the caveat that runtime validation requires extra work or compiler level hackery).
When TS 4.1 hit beta, I did a proof of concept validating that a `number` was a non-negative integer, and it works[1]. Many similar or even substantially more complex constraints are possible (I’m exploring type level JSON Schema validation of example values, and I’m not quite there but do I believe it’s possible).
I’m sure this kind of thing is child’s play in ML family languages, but it’s really cool to get this level of compile time certainty in a dynamic language.
[1]: my proof of concept was using literals without accounting for exponential representations etc, but I have no reason to doubt these are also achievable based on the ways I’ve seen 4.1+ used.
- amw-zero 6y agoI think the problem with what F* is doing is that it’s so foreign to most people that they don’t actually understand it. Typescript cannot do what you’re saying at compile time. F* knows _at compile time_ whether or not a value is within a range, and you can get a compiler error _without executing the program_.
- eyelidlessness 6y agoTypeScript can do that. That’s what I was saying.
- indolering 6y agoI'm pretty sure TypeScript 4 didn't add support for dependent types. Please cite your source or include a link to source code : ).
- amw-zero 6y agoPlease provide a code example.
- eyelidlessness 6y agoThis was my proof of concept for non-negative integers. You can clearly use the "template literal type" functionality to do arbitrary type processing. People have used it for JSON, SQL and GraphQL type validation. Validating a numerical range is trivial compared to those. type IntegerChar = | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'; type IntegerString<T extends string> = T extends IntegerChar ? T : T extends `${infer Char}${infer Rest}` ? Char extends IntegerChar ? Rest extends IntegerString<Rest> ? `${Char}${Rest}` : never : never : never; type NonNegativeInteger<T extends number> = T extends infer U ? number extends U ? never : `${U}` extends IntegerChar ? U : `${U}` extends IntegerString<`${U}`> ? U : never : never; type NonNegativeInteger<T extends number> = T extends infer U ? number extends U ? never : `${U}` extends IntegerChar ? U : `${U}` extends IntegerString<`${U}`> ? U : never : never;
- amw-zero 6y agoIt’s not difficult when you can enumerate every member of a type. Can you write a type for prime numbers this way?
- eyelidlessness 6y agoI didn’t enumerate every member though, just the digit. I’m sure it’s possible to calculate primes. The TS type system has been turing complete since 2.something, it’s just easier to use now. Edit to add: they don’t use semver, they just roll over at 9, so it’s been turning complete for ~20 releases.
- amw-zero 6y agoIn poking around, I found this: https://github.com/microsoft/TypeScript/issues/14833 https://github.com/microsoft/TypeScript/issues/14833 Where someone actually implements a compile-time check for prime numbers. I guess saying that it _can't_ be done is inaccurate. It's just absolutely horrendous.