Closed lithdew closed 6 months ago
@lithdew Hi,
Self referential recursive + indexed types are currently not supported by TypeBox.....however you can try implement this type other ways. For example, the following extracts the accounts
property out into it's own type, this avoids the need to self reference via Type.Index.
import { Type as T, Static } from '@sinclair/typebox'
type TAccounts = Static<typeof Accounts>
const Accounts = T.Recursive(This => T.Record(
T.String(),
T.Union([T.String(), T.Object({
ref: This
})
])), { $id: 'Accounts' })
type TProgram = Static<typeof Program>
const Program = T.Object({
accounts: Accounts
})
function test(value: TProgram) {
const account1 = value.accounts['account1']
if(typeof account1 === 'string') return // ignore
const account2 = account1.ref['account2']
if(typeof account2 === 'string') return // ignore
// ... and so on
}
Hope this helps S
@lithdew Hey, might close of this issue as self referential recursive indexing is generally supported (and it may not be supported in future given the way recursive types are constructed). However, you should be able to use the workaround above to represent this type.
Hope this helps. Cheers! S
I have this schema:
TProgram
resolves to:Is there any way to make this schema such that the following instance of this schema is valid?