picklemik
picklemik
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
Thanks anyway, helped a lot
18 replies
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
Okay, will check that out. For now I think I will just "replace" the arktype type function with my own scope. Like
import { scope, type as _type } from "arktype";
import { ObjectId } from "mongodb";

export const type = scope({
"string.objectId.parse": _type("string.hex").pipe((s) => new ObjectId(s)).as<ObjectId>,
objectId: _type.instanceOf(ObjectId),
}).type;

...
import { type } from "../lib/arktype.ts";
const schema = type({
id: "string.objectId.parse",
})
import { scope, type as _type } from "arktype";
import { ObjectId } from "mongodb";

export const type = scope({
"string.objectId.parse": _type("string.hex").pipe((s) => new ObjectId(s)).as<ObjectId>,
objectId: _type.instanceOf(ObjectId),
}).type;

...
import { type } from "../lib/arktype.ts";
const schema = type({
id: "string.objectId.parse",
})
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
18 replies
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
But not sure if I should create multiple scopes, or just one global scope I use in the whole app
18 replies
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
In this case e.g. it is kind of an "extension" or "plugin" where I want to provide types for mongo db
18 replies
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
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
18 replies
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
Perfect, thanks! I replaced all my extends Type with extends type.Any and I think it works now
18 replies
Aarktype
Created by picklemik on 5/3/2025 in #questions
Can't infer type of schema when using "scope"
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
import { type, scope, Type } from "arktype"

class ObjectId {
constructor(public id: string) {}
}

const Scope = scope({
objectId: type("string.hex").pipe((s) => new ObjectId(s))
})

const NotWorkingThing = Scope.type({
id: "objectId"
})

const WorkingThing = type({
id: "string.hex"
})

function whyDoesThisNotWork<T extends Type>(schema: T, fn: (v: T["infer"]) => void) {
const result = schema({ id: "12345" })
fn(result)
}

// This works with a basic type
whyDoesThisNotWork(WorkingThing, (v) => {
console.log(v.id)
})

// This is using a scope and does not work anymore
whyDoesThisNotWork(NotWorkingThing, (v) => {
console.log(v.id)
})
import { type, scope, Type } from "arktype"

class ObjectId {
constructor(public id: string) {}
}

const Scope = scope({
objectId: type("string.hex").pipe((s) => new ObjectId(s))
})

const NotWorkingThing = Scope.type({
id: "objectId"
})

const WorkingThing = type({
id: "string.hex"
})

function whyDoesThisNotWork<T extends Type>(schema: T, fn: (v: T["infer"]) => void) {
const result = schema({ id: "12345" })
fn(result)
}

// This works with a basic type
whyDoesThisNotWork(WorkingThing, (v) => {
console.log(v.id)
})

// This is using a scope and does not work anymore
whyDoesThisNotWork(NotWorkingThing, (v) => {
console.log(v.id)
})
18 replies