Replies: 6 comments 13 replies
-
There's no ability to parse function signature types because function signatures can never be validated at runtime. What you like to do with this type? |
Beta Was this translation helpful? Give feedback.
-
The best thing you could do is type as |
Beta Was this translation helpful? Give feedback.
-
More background: I am passing collections of functions into a sort of dispatcher. At runtime, the dispatcher needs to know the parameter types of the functions because it uses that information to select which function to call. But I want TypeScript to typecheck the functions that are being passed into the dispatcher at compile time. So I need the function types as static types at compile time, and a runtime-value description of them at runtime. I've looked at type reflection libraries like typescript-rtti and so far they are not up to the challenge. So I thought of turning this around and specifying the type with a runtime-value description from which TypeScript can infer the type at compile time. Seeing ArkType billed as something that can "infer TypeScript definitions 1:1" therefore was naturally very attractive. But for this use case, it would need a notation for specific function types. To make this more concrete, let's say I am trying to describe a bundle of two functions like
The Dispatcher will be generic on the type of the bundle of functions, something like
but I do want the compiler to check at compile time that the bundle of functions have the correct types, as the above code will. But now to use the bundle and call the correct function in a given circumstance when it runs, the code in Dispatcher needs to have runtime information about what the parameter types of each of the members of the bundle were. So there really needs to be a second argument like
that way the functions used to create the Dispatcher are typechecked at compile time, and the Dispatcher has a description it can compute with to use those functions at runtime. In particular, the Dispatcher will be able to determine which function in the bundle handles strings, for example, by consulting the bundleDescription. There's very little redundancy, and I get type safety at compile time and the information I need at runtime. Is this making more sense? I believe that if arktype could statically infer function types for object properties along with all of the other types it describes, it would cover exactly this need. In other words, it seems to me that arktype could also help with compile-time typechecking of code in addition to run-time validation of data. Thanks for your thoughts. |
Beta Was this translation helpful? Give feedback.
-
ArkType validators take type |
Beta Was this translation helpful? Give feedback.
-
Confused -- I got the impression that arktype inferred the described type statically at compile time. That's all I need, so I can extract the type described by the json, and then use that type as the type parameter to my generic function so it will check the types of the supplied bundle of functions at compile time. Am I mistaken that arktype generates a compile-time entity with the exact TypeScript type described by the literal argument to |
Beta Was this translation helpful? Give feedback.
-
So there's a few ideas that come to mind. One of them would be a dedicated syntax like this, which I'm planning to add after beta release as a way to create validated functions and/or typed JS: TypesInJs.mp4So that would be a possibility in terms of a function definition format that could have type metadata attached. Another option that comes to mind, depending on how much control you have over these structures, is to do something more like pattern matching, where the keys could be types themselves, and then depending on which branch the data satisfies you'd execute that handler. In terms of features that are currently available, I think the most straightforward would be to just approximate pattern matching- define the input types for the various functions, then check if each one |
Beta Was this translation helpful? Give feedback.
-
I was thinking of something along the lines of
I didn't see anything about specifying that a property should have as its value a function, but perhaps I've just missed it?
Beta Was this translation helpful? Give feedback.
All reactions