Skip to content

Index types of object's property which is array #793

Answered by sinclairzx81
hrishikesh-k asked this question in Q&A
Discussion options

You must be logged in to vote

@hrishikesh-k Hi,

You can implement this type in the following way

TypeScript Link Here

import { Type, Static } from '@sinclair/typebox'

// type T = {
//   f: ({ id: number })[]
// }

const T = Type.Object({
  f: Type.Array(Type.Object({
    id: Type.Integer()
  }))
})

// type X = {
//   p: T['f'][number]
// }

const X = Type.Object({
    p: Type.Index(Type.Index(T, Type.Literal('f')), Type.Number())
})

Unfortunately, TypeBox can't support the ['f'][number] syntax, so to access deeper nested properties, you will need to use Index multiple times. Also note, for Arrays, you can pass Type.Number() to access the element type.

Hope this helps!
S

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hrishikesh-k
Comment options

Answer selected by hrishikesh-k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants