ajv-validator / ajv-validator/ajv

LinkedList example in JTD docs gets typed as never after validation

Aperta
#2,168 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug report 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.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.

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.