A
arktypeβ€’3w ago
Micha

How to integrate generics with extend that need to access a type in scope?

queryValueObject is currently not resolving, and I assume it's because type() is creating its own scope. The question is, how can I solve it then?
scope({
queryValue: [
"null | string | number | bigint | boolean | queryValueObject | queryValue[]",
uint8Array,
dateStub,
timeStub,
module,
],
queryValueObject: {
"[string]": "queryValue",
},
namedDocument: {
read: type("<metadata extends queryValueObject>", { // queryValueObject not resolving
coll: module,
name: "string",
ts: timeStub,
"data?": "metadata",
})
},
}).export();
scope({
queryValue: [
"null | string | number | bigint | boolean | queryValueObject | queryValue[]",
uint8Array,
dateStub,
timeStub,
module,
],
queryValueObject: {
"[string]": "queryValue",
},
namedDocument: {
read: type("<metadata extends queryValueObject>", { // queryValueObject not resolving
coll: module,
name: "string",
ts: timeStub,
"data?": "metadata",
})
},
}).export();
I also tried it by wrapping it into [], but this does also not work πŸ€”
read: ["<metadata extends queryValueObject>", {
coll: module,
name: "string",
ts: timeStub,
"data?": "metadata",
}]
read: ["<metadata extends queryValueObject>", {
coll: module,
name: "string",
ts: timeStub,
"data?": "metadata",
}]
10 Replies
ssalbdivad
ssalbdivadβ€’3w ago
Haha luckily there is a much better syntax than that πŸ˜› https://arktype.io/docs/generics#scoped Although the tuple expression thing was a good intuition, because generics are only legal at the top-level of the scope, we can do something more natural with the alias keys themselves
Micha
MichaOPβ€’3w ago
oh, somehow I missed this part in the docs, although I have read over this today probably already 10 times 😬
ssalbdivad
ssalbdivadβ€’3w ago
Admittedly this part of the docs is a bit less elaborate than most of the rest because I consider both scopes and especially generics to be somewhat advanced features, but all the primary syntax is at least covered now!
Micha
MichaOPβ€’3w ago
I've updated it, but I'm still getting an error that metadata is unresolvable Type '"metadata"' is not assignable to type '"'metadata' is unresolvableβ€Š"'.ts(2322):
scope({
queryValue: [
"null | string | number | bigint | boolean | queryValueObject | queryValue[]",
uint8Array,
dateStub,
timeStub,
module,
],
queryValueObject: {
"[string]": "queryValue",
},
namedDocument: {
"read<metadata extends queryValueObject>": {
coll: module,
name: "string",
ts: timeStub,
"data?": "metadata",
}
},
}).export();
scope({
queryValue: [
"null | string | number | bigint | boolean | queryValueObject | queryValue[]",
uint8Array,
dateStub,
timeStub,
module,
],
queryValueObject: {
"[string]": "queryValue",
},
namedDocument: {
"read<metadata extends queryValueObject>": {
coll: module,
name: "string",
ts: timeStub,
"data?": "metadata",
}
},
}).export();
If I'm renaming "metadata" to "t" the error changes to Type '"t"' is not assignable to type '"true"'.ts(2322)
"read<t extends queryValueObject>": {
coll: module,
name: "string",
ts: timeStub,
"data?": "t",
}
"read<t extends queryValueObject>": {
coll: module,
name: "string",
ts: timeStub,
"data?": "t",
}
ssalbdivad
ssalbdivadβ€’3w ago
read is nested in an object there, not at the top-level of the scope
Micha
MichaOPβ€’3w ago
The way you phrase it, sounds like it's a limitation?
ssalbdivad
ssalbdivadβ€’3w ago
Well not really? It wouldn't be sensible to have a generic as the value of a property of an object the same way a Type would be What values would it allow if you used it for validation? But if your goal is to allow namedDocument.read and the . access grouping is important, the way to do that is called submodules: https://arktype.io/docs/scopes#submodules This would prevent it from accessing aliases from the top-level scope though, since it has to be parsed bottom up.
Micha
MichaOPβ€’3w ago
awesome - that is what I need. My goal was to group the types logically to keep it a bit tidy πŸ™‚
ssalbdivad
ssalbdivadβ€’3w ago
Glad you're finding a use case for all the advanced features. Let me know when you run into HKTs πŸ˜›
Micha
MichaOPβ€’3w ago
I have to say I'm quite impressed with how far I have already come without a painful workaround (I say only cyclic references + zod... πŸ˜‰ ). You did here definitely an outstanding job πŸ‘

Did you find this page helpful?