ajv-validator / ajv-validator/ajv
TS is not able to infer that an optional field might be undefined
- 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?**
v8.11.2 ( latest )
**Your typescript code**
```typescript
import Ajv, { JSONSchemaType } from "ajv";
interface MyType {
myProp?: OtherType;
}
interface OtherType {
foo: string;
bar: number;
}
const otherSchema: JSONSchemaType = {
type: 'object',
properties: {
foo: { type: 'string', minLength: 1 },
bar: { type: 'number' },
},
required: ['foo', 'bar'],
additionalProperties: false
};
const mySchema: JSONSchemaType = {
type: 'object',
properties: {
myProp: otherSchema,
},
required: [],
additionalProperties: false,
};
```
**Typescript compiler error messages**
```
app.ts:22:7 - error TS2322: Type '{ type: "object"; properties: { myProp: { type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | ... 1 more ... | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }; }; required: never[]; a...' is not assignable to type 'UncheckedJSONSchemaType'.
The types of 'properties.myProp' are incompatible between these types.
Type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType & { nullable: true; const?: null | undefined; enum?: readonly (OtherType | null | undefined)[] | undefined; default?: OtherType | ... 1 more ... | undefined; })'.
Type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }' is not assignable to type '{ $ref: string; }'.
Types of property '$ref' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
```
**Describe the change that should be made to address the issue?**
I'm not sure but I think this error occurs because TS doesn't know that `myProp` in `mySchema` might be *undefined*. The *required* array does not contain `myProp`.
I could try to assign `{ nullable: true }` to `myProp` but there is no field for `schema` so this is not possible
`myProp: { schema: otherSchema, nullable: true }`
**Are you going to resolve the issue?**
Unfortunately I don't know how.
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.