Can't infer type of schema when using "scope"
I am using arktype to parse data from incoming web requests. I define the schema and the parsed result will be passed into my handler. It looks like this and is working perfectly fine right now:
What I want to do now is change
id
in the params
schema to not be a string, but instead define my own "objectId" value. It would parse the string into a mongo ObjectId object, so I don't have to do that in the handler. I believe scopes
are what I am looking for, so I tried doing that, but it breaks my type system and I don't understand why.
I recreated the issue I have in this playground: https://tinyurl.com/arktype7 Replies
The easiest thing is probably a morph
So
type({id: ["string", "=>", parseStringToObjectId]})
Thanks, but that is not really the problem I have. I am already using morph, it has to have something to do with the scope and my generic type in the function. This is the code from the playground, there you can see the type error
Oh, right, I think you need to use
type.Any
or Type<unknown, typeof Scope>
(one/both should work)
Instead of just Type
in extends Type
Perfect, thanks! I replaced all my
extends Type
with extends type.Any
and I think it works now
Maybe one additional quick question while you are already here 🫣 Where would I best put my scope
that I would want to reuse often? Should I just put it in some util file and use that instead of the default type
when I create my types or is there something more elegant
In this case e.g. it is kind of an "extension" or "plugin" where I want to provide types for mongo db
But not sure if I should create multiple scopes, or just one global scope I use in the whole appI think a util file is way to go
Whether you want one scope or multiple is kinda down to you
I typically think of scopes as a "category" of types, and use different scopes per "category"
Fwiw iirc you can have nested scopes which is how things like
string.hex
or string.numeric.parse
are implemented
Actually, I think that's submodules which is a different thing. I forget the technical detailsOkay, will check that out. For now I think I will just "replace" the arktype
type
function with my own scope. Like
Not sure if this is a good idea in the long term, but it is easy and works for now. The string.objectId.parse
works, but I don't get auto complete for these. Maybe the nested scopes/submodules will help there
Thanks anyway, helped a lot