ajv-validator / ajv-validator/ajv-merge-patch

Type inference support

Offen
#43 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
47
Forks
19
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Let's admit a simple base schema like this one:

```ts
const SnakeSchema = {
type: 'object',
properties: {
name: { type: 'string' },
foo_bar: { type: 'string' },
},
} as const;
```

I want the type inference to work within my application, so I use the `JTDDataType` utility.

```ts
import { JTDDataType } from 'ajv/dist/jtd';

const SnakeSchema = { /* ... */ } as const;

type Snake = JTDDataType;
```

Now, let's write a simple patch:

```ts
const KebabSchema = {
$patch: {
source: SnakeSchema,
with: [
{ op: 'move', from: '/properties/foo_bar', to: '/properties/fooBar' },
],
},
} as const;
```

The only way I found to get the infered type is by doing this:

```ts
type Kebab = Omit & {
fooBar: SnakeSchema['foo_bar'];
```

As it is not really DRY, I was wondering if there is something I miss in the docs... If not, maybe is there something that can be done in the project?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

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