genotype_cli 0.17.0

Genotype language CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# TypeScript Target Overview

Genotype generates idiomatic TypeScript code that can be used directly in the application or published as a package.

This overview shows how Genotype types translate to TypeScript, target-specific features and configuration options.

See the [Quick Genotype Language Tour](language.md) for the language overview and [TypeScript Configuration](typescript-configuration.md) for detailed configuration reference.

## Translation

The following examples give an overview of how Genotype translates into TypeScript.

**Note:**

Object examples show [Interfaces and
Aliases](typescript-configuration.md#tsprefer---interfaces-or-type-aliases); other
examples use Types. All include [Zod
output](typescript-configuration.md#tsmode---generation-mode). Feature examples omit
imports; Zod examples use `z` from [Zod](https://www.npmjs.com/package/zod). The [Complete
Module](#complete-module) includes imports.

### Complete Module

This source combines an object, a literal field, an optional field, a record, and `any`:

```type
Book: {
  kind: "book",
  displayTitle: string,
  subtitle?: string,
  ratings: { []: int },
  extra: any,
}
```

The generated module includes its definitions and any required imports:

**Interfaces**

```ts
export interface Book {
  kind: "book";
  displayTitle: string;
  subtitle?: string | undefined;
  ratings: Record<string, number>;
  extra: any;
}
```

**Aliases**

```ts
export type Book = {
  kind: "book";
  displayTitle: string;
  subtitle?: string | undefined;
  ratings: Record<string, number>;
  extra: any;
};
```

**Zod**

```ts
import { z } from "zod";

export const Book = z.object({
  kind: z.literal("book"),
  displayTitle: z.string(),
  subtitle: z.union([z.string(), z.undefined()]).optional(),
  ratings: z.record(z.string(), z.number()),
  extra: z.any(),
});

export type Book = z.infer<typeof Book>;
```

### Unions

Genotype unions translate directly to TypeScript union types.

```type
Value: string | int
```

**Types**

```ts
export type Value = string | number;
```

**Zod**

```ts
export const Value = z.union([z.string(), z.number()]);

export type Value = z.infer<typeof Value>;
```

### Primitives

#### Numeric Types

##### `number`

Genotype `number` translates directly to the TypeScript `number` type.

```type
Amount: number
```

**Types**

```ts
export type Amount = number;
```

**Zod**

```ts
export const Amount = z.number();

export type Amount = z.infer<typeof Amount>;
```

##### `int`

Genotype `int` translates into the TypeScript umbrella `number` type.

```type
Count: int
```

**Types**

```ts
export type Count = number;
```

**Zod**

```ts
export const Count = z.number();

export type Count = z.infer<typeof Count>;
```

##### `float`

Genotype `float` translates into the TypeScript umbrella `number` type.

```type
Ratio: float
```

**Types**

```ts
export type Ratio = number;
```

**Zod**

```ts
export const Ratio = z.number();

export type Ratio = z.infer<typeof Ratio>;
```

##### Sized Numeric Types

Sized Genotype numeric types translate into the TypeScript umbrella `number` type, except for `i128` and `u128`, which translate into `bigint`.

```type
SmallCount: i16
PreciseRatio: f32
LargeCount: i128
```

**Types**

```ts
export type SmallCount = number;

export type PreciseRatio = number;

export type LargeCount = bigint;
```

**Zod**

```ts
export const SmallCount = z.number();

export type SmallCount = z.infer<typeof SmallCount>;

export const PreciseRatio = z.number();

export type PreciseRatio = z.infer<typeof PreciseRatio>;

export const LargeCount = z.bigint();

export type LargeCount = z.infer<typeof LargeCount>;
```

#### Booleans

Genotype `boolean` translates directly to the TypeScript `boolean` type.

```type
Ready: boolean
```

**Types**

```ts
export type Ready = boolean;
```

**Zod**

```ts
export const Ready = z.boolean();

export type Ready = z.infer<typeof Ready>;
```

#### Strings

Genotype `string` translates directly to the TypeScript `string` type.

```type
Title: string
```

**Types**

```ts
export type Title = string;
```

**Zod**

```ts
export const Title = z.string();

export type Title = z.infer<typeof Title>;
```

#### Literal Types

Genotype literal types translate directly to TypeScript literal types.

```type
Category: "fiction"
```

**Types**

```ts
export type Category = "fiction";
```

**Zod**

```ts
export const Category = z.literal("fiction");

export type Category = z.infer<typeof Category>;
```

#### Null

Genotype `null` translates directly to the TypeScript `null` type.

```type
Empty: null
```

**Types**

```ts
export type Empty = null;
```

**Zod**

```ts
export const Empty = z.literal(null);

export type Empty = z.infer<typeof Empty>;
```

#### Branded Primitives

Genotype branded primitives translate into branded TypeScript types. Their runtime values remain unchanged.

```type
BookId: @string
```

**Types**

```ts
export type BookId = string & { [bookIdBrand]: true };
declare const bookIdBrand: unique symbol;
```

**Zod**

```ts
export const BookId = z.string().brand<"BookId">();

export type BookId = z.infer<typeof BookId>;
```

### Composite Types

#### Objects

Genotype objects translate into TypeScript interfaces or type aliases. Choose the output style with [ts.prefer](typescript-configuration.md#tsprefer---interfaces-or-type-aliases).

```type
Book: { title: string }
```

**Interfaces**

```ts
export interface Book {
  title: string;
}
```

**Aliases**

```ts
export type Book = {
  title: string;
};
```

**Zod**

```ts
export const Book = z.object({
  title: z.string(),
});

export type Book = z.infer<typeof Book>;
```

##### Optional Object Fields

Optional Genotype object fields translate into optional TypeScript properties.

```type
Draft: { subtitle?: string }
```

**Interfaces**

```ts
export interface Draft {
  subtitle?: string | undefined;
}
```

**Aliases**

```ts
export type Draft = {
  subtitle?: string | undefined;
};
```

**Zod**

```ts
export const Draft = z.object({
  subtitle: z.union([z.string(), z.undefined()]).optional(),
});

export type Draft = z.infer<typeof Draft>;
```

##### Object Extensions

Genotype object extensions translate into interface inheritance or type intersections.

```type
Named: { name: string }
NamedBook: { ...Named, pages: int }
```

**Interfaces**

```ts
export interface Named {
  name: string;
}

export interface NamedBook extends Named {
  pages: number;
}
```

**Aliases**

```ts
export type Named = {
  name: string;
};

export type NamedBook = Named & {
  pages: number;
};
```

**Zod**

```ts
export const Named = z.object({
  name: z.string(),
});

export type Named = z.infer<typeof Named>;

export const NamedBook = Named.extend({
  pages: z.number(),
});

export type NamedBook = z.infer<typeof NamedBook>;
```

#### Arrays

Genotype arrays translate into TypeScript `Array<T>` types.

```type
Titles: [string]
```

**Types**

```ts
export type Titles = Array<string>;
```

**Zod**

```ts
export const Titles = z.array(z.string());

export type Titles = z.infer<typeof Titles>;
```

#### Tuples

Genotype tuples translate directly to TypeScript tuple types.

```type
Point: (float, float)
```

**Types**

```ts
export type Point = [number, number];
```

**Zod**

```ts
export const Point = z.tuple([z.number(), z.number()]);

export type Point = z.infer<typeof Point>;
```

#### Records

Genotype records translate into TypeScript `Record<K, V>` types. An omitted key type means string keys.

```type
Scores: { []: int }
```

**Types**

```ts
export type Scores = Record<string, number>;
```

**Zod**

```ts
export const Scores = z.record(z.string(), z.number());

export type Scores = z.infer<typeof Scores>;
```

### Special Data Types

#### Any Type

Genotype `any` translates directly to the TypeScript `any` type.

```type
Payload: any
```

**Types**

```ts
export type Payload = any;
```

**Zod**

```ts
export const Payload = z.any();

export type Payload = z.infer<typeof Payload>;
```

### Generic Types

Genotype generic types translate into TypeScript generic types. Zod output uses functions accepting schemas, e.g., `Envelope(Book)`.

```type
Envelope<Body>: { body: Body }
```

**Interfaces**

```ts
export interface Envelope<Body> {
  body: Body;
}
```

**Aliases**

```ts
export type Envelope<Body> = {
  body: Body;
};
```

**Zod**

```ts
export const Envelope = <Body extends z.ZodTypeAny>(Body: Body) =>
  z.object({
    body: Body,
  });

export type Envelope<Body extends z.ZodTypeAny> = z.infer<ReturnType<typeof Envelope<Body>>>;
```

### Recursive Types

Genotype recursive types translate directly to TypeScript recursive types. Zod output uses getters or lazy schemas.

```type
LinkedNode: { value: string, next?: LinkedNode }
```

**Interfaces**

```ts
export interface LinkedNode {
  value: string;
  next?: LinkedNode | undefined;
}
```

**Aliases**

```ts
export type LinkedNode = {
  value: string;
  next?: LinkedNode | undefined;
};
```

**Zod**

```ts
export const LinkedNode = z.object({
  value: z.string(),
  get next() {
    return z.union([LinkedNode, z.undefined()]).optional();
  },
});

export type LinkedNode = z.infer<typeof LinkedNode>;
```

### Annotations

Annotations intended for other targets, such as Rust enum variant names, don't affect TypeScript output.

## Configuration

Use `[ts]` in `genotype.toml` to configure TypeScript. See the [TypeScript Configuration Reference](typescript-configuration.md) for more details.

See [Genotype Configuration](configuration.md) for global settings and [Common Target Options](configuration.md#common-target-options).