-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typesafe dates #7
Comments
Cool idea. :)
So, removing the quotes and optionally turning the object into a tuple yields this error:
Edit: It'd be cool to get the type level on par with the run-time level so we could e.g. do the built-in functions on the type level as well. the bottle-neck there is the current lack of type level arithmetic operators though. The TS team doesn't want them though (ref); I looked into it but figured it'd take a bit of effort to put them in as well. |
Hey thanks for the reply! Yeah it would be awesome to have some form of dependent types in Typescript, similar to the DOT calculus does for Scala. Being able to run functions at compile-time would be amazing too. This could probably be done with a pre-processor similar to Template Haskell and would probably be pretty easy to implement for pure functions where the arguments are known at compile-time. |
Running functions at compile-time sounds like executing user JS rather than calculating types. I have tried something related though, calculating the return types of functions at the type level (to use in other types), see microsoft/TypeScript#17961. I hadn't managed to finish it yet though.
I'd checked a book on DOT with Idris for ideas in that area and how they might apply here. I think the textbook example of fixed-length vectors has been possible already. One of the more interesting things they were doing though was type constraints such as to create type-safe division, with a custom user constraint to e.g. disallow |
Do you know if there's a way at the moment to type a function similar to this:
I'd like the return type to depend on the input type, something like Haskell's type families would do the trick I think:
I am not that experienced yet in TypeScript so maybe this can already be done in some way? EDIT:
|
It seems in your example inference is getting confused by the ternary operator. Technically it should be following that using a type guard. See one related(-ish?) issue here. It actually sounds like you might be able to just open a new issue over at TS for that. Full-fledged type-level type checks aren't possible yet (in my PR above I wanted to do that by hooking into e.g. the interface you showed there). That being said, for this easier case concerning simple types, there has been a hack involving writing stuff to the global class types as demonstrated e.g. here, the reasoning being that we can do checks on string literal types, even though we cannot do checks on types in a more general fashion. |
I had another question/puzzle for you. I have the following piece of code (attempt at type-safe Redux):
I got it almost working except for the use of
Do you know of any way to fix this? Another problem is that the types depend on the very specific Actions while I would like it to be any Actions object given by the users. I don't think this is solvable though... |
|
I hope the original question is resolved here, then I'll mark as closed. |
A way to encode dates on the typelevel is something like this:
Do you know a way to do this more concisely or with numbers instead of strings?
The text was updated successfully, but these errors were encountered: