:point_up_2: I wrote a quick <getValidatedArgsOrTh...
# orm-help
r
👆 I wrote a quick getValidatedArgsOrThrow validation function that leverages TypeScript’s code flow analysis to infer the type correctly. This seems the safest to me. Curious to hear any thoughts. (in my case I want the JSON to be an object and nothing else; works the same if you expect an array or similar)
prisma rainbow 1
j
Go next-level by wrapping your functions, and VS Code will be able to infer your types!
It’d be a good spot for a decorator if you use classes.
r
@Jason Kleinberg Thanks for the pointer. Would love to check out your link, but it redirects me to carbon’s homepage — is the link broken?
It’d be a good spot for a decorator if you use classes.
Not using classes! But I agree, the validation could well be wrapped; will need to understand different use cases of different resolvers a bit better though. Thanks, really appreciate it. 🙏
j
Try that one.
I believe that this should basically be a universal argument validator, since it leaves the implementation up to the end user, and just handles all of the type inference.
r
Thanks I will read through it carefully when back at the computer
j
I hope it’s helpful or at least interesting. It was a fun two hours “solving” the problem. I didn’t write real tests for it, so it might not work at all for all I really know 😅. All of the types are happy at least.
r
Interesting for sure! So much I can tell after peaking at it.
But would like to understand the type inference in there and not good enough with typescript to do that from mobile haha
I'm also more concerns about types right now, but might check out how this works with tests
If I find the time this week, I'll report back here 🤓
j
I wrote out the type arguments in the key function as full names so that it should be easy to trace them through.
I don’t know why that is such an uncommon practice.
Whoops! I put the JSDoc comment on the invocation, not the implementation… 😅
I might be easier to understand to look at it converted to JS, just to get the types out of the way.
Basically the idea is that when you have a function as an argument, TS can fill in the different type-arguments. The validator argument has to make a type-assertion for the same type as the resolver-argument. It basically returns a function that when run will 1. run the
validator
on the resolver-argument (
args
), which is expected to either
• throw an error when the resolver-argument doesn’t pass the tests
• do nothing otherwise.
2. Use the now type-assured
args
in the defined resolver. 3. Update the name of the function to note it is a validated version of the
handler
function.
Ugh, Slack’s formatting is awful.
r
It quite sure how 2. works if we're not returning anything from The validator func though. 🤨
j
It doesn’t have to. Look at the type signature. Notice it’s return signature?
It’s a
TypeCheck
, which asserts that a value is a given type.
Copy code
type TypeCheck<T> = (typeName: string, value: unknown) => asserts value is T
r
Yea but that's actually what I like to avoid
j
What is?
r
Because that's relying on the developer to cast the type correctly
And I'd like typescript to do that job for me because I think devs can error more often than ts
j
No. You can even automate these type validators.
r
Now I'm curious
j
r
Ok I think I got it
By asserting the generic we tell TS that even though we're not returning anything, please remember what the type at return was so it can use it elsewhere
Ha, that's convenient actually. I think I like this better
j
Sort of… It’s looking at the type of the
arg
variable, and assuming when the validator passes, that it is the correct type.
👍 1
r
Will TS pick up the ajv validation? Also very interesting
j
It’s just a function that runs and either throws a exception or doesn’t. 🤷
Use whatever function that you like for that. 😄
You don’t have to tell it what your assertion type is, because it’s just inferred when you called
ArgumentsValidated
.
r
Thanks, @Jason Kleinberg learned a lot from your input here today. :)
j
Excellent! You’re very welcome. I’ve been working on something very similar, so I had been thinking in these lines.
Check out Pal.js too… It may just do what you want.