Closed hpeebles closed 1 week ago
@hpeebles Hi, you can generate the correct types using the following.
export type MyFancyRecord = Record<string, string>
export type MyOtherFancyRecord = Record<number, string>
The output you see for { [key: string]: string }
is a current design limitation in TypeBox.
Consider the following...
type T = {
[key: string]: number, // additionalProperties
x: number,
y: number,
z: number
}
which maps into the following
type T = Static<typeof T>
const T = Type.Object(
{
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
},
{
additionalProperties: Type.Number()
}
)
... but where additionalProperties
does not currently contribute to type inference (which is at the core of this issue). For now the recommendation is to use Record instead of the literal type expression.
Additionally, If you need a structure similar to the T
type above, you can use.
type T = {
x: number,
y: number,
z: number
} & Record<string, number>
which maps to
type T = Static<typeof T>
const T = Type.Intersect([
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
}),
Type.Record(Type.String(), Type.Number())
])
The Workbench will be updated with a solution to this once TypeBox implements additional inference logic for additionalProperties.
Hope this helps S
Please see the example below.
Input:
Output:
Desired output: