ajv-validator / ajv-validator/ajv

TS is not able to infer that an optional field might be undefined

Abierto
#2,180 5 comentarios 0 reacciones 0 asignados Ver en GitHub
typescript
Lenguaje dominante
TypeScript
Estrellas
14.8k
Forks
1k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.