ajv-validator / ajv-validator/ajv

Type-safe intersections?

Ouverte
#2,000 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
limitation typescript
Langage dominant
TypeScript
Étoiles
14.8k
Forks
1k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

```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?

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.