ajv-validator / ajv-validator/ajv

Type-safe intersections?

オープン
#2,000 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
limitation typescript
主要言語
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 はまだ評価されていません。

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

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