Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow readonly arrays in interfaces #1448

Merged
merged 1 commit into from
Feb 16, 2021

Conversation

LinusU
Copy link
Contributor

@LinusU LinusU commented Feb 16, 2021

Before this change, a readonly array would be inferred as "object":

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".


What issue does this pull request resolve?

#1447

What changes did you make?

Changed T extends any[] to T extends readonly any[]. This works since readonly T[] is a subset of T[], and thus T[] always satisfies readonly T[].

Is there anything that requires more attention while reviewing?

The one word changed 😄

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".
@epoberezkin
Copy link
Member

that's because of the conditional fall-through to objects - at first I thought there is something more weird going on :)

Thank you!

@epoberezkin epoberezkin merged commit e63771c into ajv-validator:master Feb 16, 2021
@LinusU
Copy link
Contributor Author

LinusU commented Feb 16, 2021

Thanks for the quick merge ❤️

When do you think this will be part of a release? I'm currently blocked on this 😅

@LinusU LinusU deleted the patch-1 branch February 16, 2021 18:22
@epoberezkin
Copy link
Member

today then ;)

@epoberezkin
Copy link
Member

https://github.com/ajv-validator/ajv/releases/tag/v7.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants