ajv-validator / ajv-validator/ajv
Type-safe intersections?
- 主要语言
- TypeScript
- 星标
- 14.8k
- 派生
- 1k
- PR 合并指标
- 30 天内没有已合并 PR
描述
```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?
贡献指南
评估
这个 Issue 还没有评估数据。