ajv-validator / ajv-validator/ajv

LinkedList example in JTD docs gets typed as never after validation

オープン
#2,168 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug report typescript
主要言語
TypeScript
スター
14.8k
フォーク
1k
PR マージ指標
30日以内にマージされた PR はありません

説明

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

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。