ajv-validator / ajv-validator/ajv

LinkedList example in JTD docs gets typed as never after validation

Offen
#2,168 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug report typescript
Vorherrschende Sprache
TypeScript
Sterne
14.8k
Forks
1k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

8.11.2 (latest as of this writing)

**Ajv options object**

```ts
import Ajv, { JTDSchemaType } from "ajv/dist/jtd";
const ajv = new Ajv();
```

**JTD Schema**

From https://ajv.js.org/json-type-definition.html#ref-schemas

```ts

type LinkedList = { val: number; next?: LinkedList };

const schema: JTDSchemaType = {
definitions: {
node: {
properties: {
val: { type: "float64" }
},
optionalProperties: {
next: { ref: "node" }
}
}
},
ref: "node"
};
```

**Sample data**

```ts
const data = { val: 1 } as LinkedList;
```

**Your code**

```ts
const validate = ajv.compile(schema);

if (validate(data)) {
const next: LinkedList | undefined = data.next;
console.log(next);
}
```

https://replit.com/@ento/IntrepidIndolentOutput#linkedlist.ts

^`strictNullChecks` is enabled in `tsconfig.json`

```json
"strictNullChecks": true,
```

**Validation result, data AFTER validation, error messages**

```
// on data.next
Property 'next' does not exist on type 'never'.
```

**What results did you expect?**

I expected the type of `data` to be `LinkedList` after being narrowed by `validate(data)`

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

I was exploring how JTDSchemaType works and noticed that the example in the documentation doesn't seem to work. I'm not in immediate need for this to work.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.