Referencing self

Hello, I have a type A
const A = {
hello: 'string',
aArray: arrayOf(???)
}
const A = {
hello: 'string',
aArray: arrayOf(???)
}
How can I reference itself in arrayOf? A doesn't seem to work.
3 Replies
ssalbdivad
ssalbdivad7mo ago
So first I'd recommend upgrading to 2.0.0-dev.10 which was just released. In the next ~week there will be a this keyword that allows you to reference the current type:
const A = type({
hello: "string",
aArray: "this[]"
})
const A = type({
hello: "string",
aArray: "this[]"
})
Until then, you will need to use a scope:
const types = scope({
someObj: {
hello: "string",
aArray: "someObj[]"
}
}).export()

types.someObj({...})
const types = scope({
someObj: {
hello: "string",
aArray: "someObj[]"
}
}).export()

types.someObj({...})
The other nice thing about a scope is that you can then reuse someObj in other string definitions as well in the same scope, so it makes your types composable in expressions.
JameEnder
JameEnderOP7mo ago
Thank you!
ssalbdivad
ssalbdivad7mo ago
Also a couple of the big changes while migrating: Helper methods like union and intersection are now chained directly off a type instance like type({a: "string"}).or({b: "number"}) Instead of a discriminated union like { problems, data} or {out, errors}, the default type invocation now returns YourOutput | ArkErrors. You can still check if your result is valid with a simple expression like:
const user = type({
name: "string",
age: "number"
})
const out = user({})

// type.errors is an alias for ArkErrors, the familiar array with a .summary
if(out instanceof type.errors) {
console.log(out.summary)
} else {
console.log(`Hello, ${out.name}`)
}
const user = type({
name: "string",
age: "number"
})
const out = user({})

// type.errors is an alias for ArkErrors, the familiar array with a .summary
if(out instanceof type.errors) {
console.log(out.summary)
} else {
console.log(`Hello, ${out.name}`)
}
More docs coming soon!
Want results from more Discord servers?
Add your server