ajv-validator / ajv-validator/ajv

Type-safe intersections?

Đang mở
#2,000 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
limitation 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ả

```ts
export type StepBase = { id: string; blockId: string; outgoingEdgeId?: string }
const stepBaseSchema: JSONSchemaType = {
type: 'object',
properties: {
id: { type: 'string' },
blockId: { type: 'string' },
outgoingEdgeId: { type: 'string', nullable: true },
},
required: ['id', 'blockId'],
}

export type StartStep = StepBase & {
type: 'start'
label: string
}
const startStepSchema: JSONSchemaType = {
type: 'object',
properties: {
...stepBaseSchema.properties,
type: { type: 'string', enum: ['start'] },
label: { type: 'string' },
},
required: [...stepBaseSchema.required, 'label'],
}
```

Is there a way to create a schema for an intersection type without repeating the code?

The above code says: `The types of 'properties.id' are incompatible between these types.`

For union type we can use `anyOf` but what about intersection types? I tried using `allOf` but it doesn't seem to be appropriate?

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.