A
arktype•3mo ago
PIat

Extracting intent into runtime

Hello! What is the canon way to extract keys from types like these:
const type1 = type({
intent: '"hello"',
})

const type2 = type({
intent: '"hello"',
}).or({
intent: '"bye"',
})
const type1 = type({
intent: '"hello"',
})

const type2 = type({
intent: '"hello"',
}).or({
intent: '"bye"',
})
In a way that the resulting object is
{
intent: {
hello: 'hello'
}
}

{
intent: {
hello: 'hello',
bye: 'bye'
}
}
{
intent: {
hello: 'hello'
}
}

{
intent: {
hello: 'hello',
bye: 'bye'
}
}
? Would it be achieved with internal.distribute?
5 Replies
ssalbdivad
ssalbdivad•3mo ago
Yeah you'd have to write a custom distribute that's not exactly a standard operation
PIat
PIatOP•3mo ago
Got it!
const intent2 = type2.in.internal.distribute(
(branch) => {
return branch.required().get('intent').unit
},
(branches) =>
branches.reduce((acc, intent) => ({ ...acc, [intent]: intent }), {}),
)
const intent2 = type2.in.internal.distribute(
(branch) => {
return branch.required().get('intent').unit
},
(branches) =>
branches.reduce((acc, intent) => ({ ...acc, [intent]: intent }), {}),
)
And the type:
intent: { [I in type.infer.In<def>['intent']]: I }
intent: { [I in type.infer.In<def>['intent']]: I }
ssalbdivad
ssalbdivad•3mo ago
Nice! That looks pretty clean 🧹
PIat
PIatOP•3mo ago
Is there a key for both required() and optional()? Or is it just [...branch.required(), ...branch.optional()]?
ssalbdivad
ssalbdivad•3mo ago
Yeah you should look at StructureNode
Want results from more Discord servers?
Add your server