Skip to content

Commit

Permalink
fix: allow readonly arrays in interfaces
Browse files Browse the repository at this point in the history
Before this change, a readonly array would be inferred as "object":

```ts
interface TransactionInput {
  items: readonly { count: number, productId: string }[]
}

const transactionInputValidator = ajv.compile<TransactionInput>({
  additionalProperties: false,
  properties: {
    items: { type: 'array' }, // <--- Error: TS wants this to be "object"
  },
  required: ['items'],
  type: 'object'
})
```

Now this is properly inferred as "array".
  • Loading branch information
LinusU authored Feb 16, 2021
1 parent 6926312 commit 3468e93
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/types/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type JSONSchemaType<T, _partial extends boolean = false> = (T extends num
} & {length: T["length"]}
minItems: T["length"]
} & ({maxItems: T["length"]} | {additionalItems: false})
: T extends any[]
: T extends readonly any[]
? {
type: JSONType<"array", _partial>
items: JSONSchemaType<T[0]>
Expand Down

0 comments on commit 3468e93

Please sign in to comment.