ajv-validator / ajv-validator/ajv

Type of sibling property of a ref property is unknown when validated via JTDSchemaType

Đang mở
#2,167 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug report typescript
Ngôn ngữ chính
TypeScript
Star
14.8k
Fork
1k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

```ts
type MyType = {
referenced: number;
stringProp: string;
};

const schema: JTDSchemaType = {
definitions: {
num: { type: "int32" }
},
properties: {
referenced: { ref: "num" },
stringProp: { type: "string" }
}
} as const;

```

**Sample data**

```ts
const data = {} as any;
```

**Your code**

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

if (validate(data)) {
const revealType: never = data;
const stringProp: string = data.stringProp;
console.log(stringProp);
}
```

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

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

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

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

TypeScript's typecheck errors:

```
// const revealType
Type '{ referenced: number; stringProp: unknown; } & {}' is not assignable to type 'never'.

// const stringProp
Type 'unknown' is not assignable to type 'string'.
```

**What results did you expect?**

The type of `stringProp` to be `string` after being narrowed by `validate(data)`

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

My current workaround is to hold the definition in a const and use it to compose the schema object. (The actual code is a bit more complex than this.)

```ts
const numSchema = { type: "int32" } as const;

const schema: JTDSchemaType = {
properties: {
referenced: numSchema,
stringProp: { type: "string" }
}
} as const;
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.