ajv-validator / ajv-validator/ajv

JSONSchemaType<T> fails compilation when T is a generic type

Aperta
#2,202 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub
limitation typescript
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

8.12.0

**Your typescript code**

[Playground](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgKQMoHkByqDGALAUxAEMAVATzALgF84AzKCEOAcmICsA3VgKF4IA7AK4sAolyHwEvOHLgAxdOjgBeNvQgRWAGlnyAQgEEASmrYAjYlF28a-YIJgEo9YjmoAFYuQA2EYgATRH05AG0JKQA6JXQAXQAuEPkUuGJfYA8kgGcYKEcAcz0UmgBuULgIySco4xNE5NS5CwgLHLzC4vkyu34YSi8ffyDcQhJzGRSwgmr4RzhIpwa0LFGiMgGAHm8-AMDp2biAPnKe3hwIQVy4MCG9tZIkneHAh+IJiqro2IbJprh+lQkqxWhwCDgYLZ-jcmFRYMACNkkn9oXJ0pkCMiAQNgbl8oICqxaF0mjQSakoAQAI7CYCUwJJMLsDIeVhxclkz6LGC1Uy-CopQGYtig8GQ8kpMCwlwwBFIxqouAtNqIbFAth4wpEznQnX-Sk0ukEBmVVjKtkcvT2XiOZyudzUACyiOyxAKBE2YjgBAAHs5BIFsgtZkcFd7ZkkxOVJXcgk9Y698OsInFTn0BnBndlXe63p7vX6hIHg1JQ+oVtgkyQKFRNlmcx6xEcTvxfZBYHALld4O6YPW3QQ3uZ877-cXuUcABQVGZSGvCsS8ACUSX7uarxE9ZdDKLglJgwiggjDgpxIosYIhUP+UogcNliORAv+s6cWKFuI6BN03pEIEZr4wPOcTEs+TS3LscY3AmbwHHOAzsmBoHQgatL0oyrCAT+rAQS8FoVD0PRAA)

```typescript
import { JSONSchemaType } from 'ajv'

enum Event {
FOO = 'foo',
BAR = 'bar',
}

interface Payload {
[Event.FOO]: {
alice: string,
};
[Event.BAR]: {
bob: string,
};
}

type PayloadSchema = {
[event in Event]: JSONSchemaType;
};

const payloadSchema: PayloadSchema = {
[Event.FOO]: {
type: 'object',
properties: {
alice: { type: 'string' },
},
required: ['alice'],
},
[Event.BAR]: {
type: 'object',
properties: {
bob: { type: 'string' },
},
required: ['bob'],
},
}

interface Message {
event: E;
payload: PayloadSchema[E];
}

type MessageSchema = JSONSchemaType>;

export const getMessageSchema = (
eventType: E
): MessageSchema => {
return {
type: 'object',
properties: {
event: { type: 'string', enum: [eventType] },
payload: payloadSchema[eventType],
},
required: ['event', 'payload'],
};
};
```

**Typescript compiler error messages**

```
Type '{ type: "object"; properties: { payload: PayloadSchema[E]; }; required: "payload"[]; }' is not assignable to type 'MessageSchema'.
Object literal may only specify known properties, and 'type' does not exist in type 'never'.
```

**Describe the change that should be made to address the issue?**

`JSONSchemaType` should recognize that the type `Message` is an object type.

Adding the following test:

```typescript
type Test = MessageSchema extends Record ? true : false;

export const getMessageSchema = (
eventType: E
): MessageSchema => {
const test: Test = true;
...
}
```

reveals that the constraint `MessageSchema extends Record` is satisfied, and I expect the branch of `UncheckedJSONSchemaType` that checks `T extends Record` to match and result in the schema type for records and dictionaries.

**Are you going to resolve the issue?**

Not yet.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.