ajv-validator / ajv-validator/ajv

Type-safe intersections?

Offen
#2,000 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
limitation typescript
Vorherrschende Sprache
TypeScript
Sterne
14.8k
Forks
1k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.