A
arktype•2y ago
Dimava

How to access current scope types with `type()` / `morph()` / `narrow()` / `arrayOf()` inside scope?

How to access current scope types with type() / morph() / narrow() / arrayOf() inside scope?
const $ = scope({
blah: "1 < number < 10",
blahArray: () => $.type("blah[]")
})

const types = $.compile()
const $ = scope({
blah: "1 < number < 10",
blahArray: () => $.type("blah[]")
})

const types = $.compile()
1 Reply
ssalbdivad
ssalbdivad•2y ago
Looks like you updated the question to include my response! Thank you 🙂 So yes, for anyone wondering the approach @Dimava mentioned will work. It's actually one of the reasons you have to "compile" a scope in the first place. If there weren't a compile step, $.type would give a "can't access property 'type' on undefined" error since the assignment hasn't occurred yet. This also works for recursive types somehow, e.g. this is fully inferred:
const $ = scope({
a: () => $.type({ b: "b" }),
b: () => $.type({ a: "a" })
})
const types = $.compile()
const $ = scope({
a: () => $.type({ b: "b" }),
b: () => $.type({ a: "a" })
})
const types = $.compile()
TS has definitely given up trying to resolve circular reference errors on much less convoluted cases than this, but I am glad it works 😊
Want results from more Discord servers?
Add your server