UNPKG

34.4 kBMarkdownView Raw
1_Auto-generated file. Updated with NPM deploy. Update manually with 'yarn docs'._
2
3# typescript-json-schema test examples
4
5## [abstract-class](./test/programs/abstract-class)
6
7```ts
8export abstract class AbstractBase {
9 propA:number;
10 propB:string;
11
12 abstract doNotInclude(): void;
13}
14
15```
16
17
18## [abstract-extends](./test/programs/abstract-extends)
19
20```ts
21import { AbstractBase } from "../abstract-class/main";
22
23class MyObjectFromAbstract extends AbstractBase {
24 doNotInclude(): void { }
25
26 propB:string;
27 propC:number;
28}
29```
30
31
32## [annotation-default](./test/programs/annotation-default)
33
34```ts
35interface MyObject {
36 /**
37 * @default true
38 */
39 varBoolean: boolean;
40 /**
41 * @default 123
42 */
43 varInteger: number;
44 /**
45 * @default 3.21
46 */
47 varFloat: number;
48 /**
49 * @default "foo"
50 */
51 varString: string;
52 /**
53 * @default [true, false, true]
54 */
55 varBooleanArray: boolean[];
56 /**
57 * @default [1, 2, 3, 4, 5]
58 */
59 varIntegerArray: number[];
60 /**
61 * @default [1.23, 65.21, -123.40, 0, 1000000.0001]
62 */
63 varFloatArray: number[];
64 /**
65 * @default ["a", "b", "c", "..."]
66 */
67 varStringArray: string[];
68 /**
69 * @default [true, 123, 3.21, "foo"]
70 */
71 varMixedArray: any[];
72}
73```
74
75
76## [annotation-id](./test/programs/annotation-id)
77
78```ts
79/**
80 * @$id filled#
81 */
82interface MySubObject {
83 a: boolean;
84}
85
86interface MyObject {
87 /**
88 * @$id empty#
89 */
90 empty;
91
92 filled: MySubObject;
93}
94```
95
96
97## [annotation-items](./test/programs/annotation-items)
98
99```ts
100interface MyObject {
101 /**
102 * @items {"type":"integer"}
103 */
104 a: number[];
105
106 /**
107 * @items {"type":"integer", "minimum":0}
108 */
109 b: number[];
110
111 /**
112 * @items.type integer
113 * @items.minimum 0
114 */
115 c: number[];
116
117 /**
118 * @items.type integer
119 */
120 d: number[];
121
122 /**
123 * @items {"type":"string", "format":"email"}
124 */
125 emails: string[];
126
127 /**
128 * @items.type string
129 * @items.format email
130 */
131 emails2: string[];
132
133}
134```
135
136
137## [annotation-ref](./test/programs/annotation-ref)
138
139```ts
140interface MySubObject {}
141
142interface MyObject {
143 /**
144 * @$ref http://my-schema.org
145 */
146 externalRef;
147
148 /**
149 * @$ref http://my-schema.org
150 */
151 externalRefOverride: MySubObject;
152}
153```
154
155
156## [annotation-required](./test/programs/annotation-required)
157
158```ts
159import { MyDefaultObject, MySubObject2 } from "./main";
160
161export const mySubObject2Example: MySubObject2[] = [{
162 bool: true,
163 string: "string",
164 object: { prop: 1 }
165}];
166
167const myDefaultExample: MyDefaultObject[] = [{
168 age: 30,
169 name: "me",
170 free: true
171}]
172
173export default myDefaultExample;
174```
175
176
177## [annotation-required](./test/programs/annotation-required)
178
179```ts
180interface MySubObject {
181 bool: boolean;
182 string: string;
183 object: object | null;
184 /**
185 * @examples require('./examples.ts').mySubObject2Example
186 */
187 subObject?: MySubObject2;
188}
189
190export interface MySubObject2 {
191 bool: boolean;
192 string: string;
193 object: object;
194}
195
196export interface MyDefaultObject {
197 age: number;
198 name: string;
199 free?: boolean;
200}
201
202export interface MyObject {
203 /**
204 * @examples require(".").innerExample
205 */
206 filled: MySubObject;
207 /**
208 * @examples require('./examples.ts')
209 */
210 defaultObject: MyDefaultObject;
211}
212
213export const innerExample: MySubObject[] = [
214 {
215 bool: true,
216 string: "string",
217 object: {}
218 },
219];
220```
221
222
223## [annotation-title](./test/programs/annotation-title)
224
225```ts
226/**
227 * @title filled#
228 */
229interface MySubObject {
230 a: boolean;
231}
232
233interface AnotherSubObject {
234 b: boolean;
235}
236
237interface MyObject {
238 /**
239 * @title empty#
240 */
241 empty;
242 /**
243 * @title filled
244 */
245 filled: MySubObject;
246 nonTitled: AnotherSubObject;
247}
248```
249
250
251## [annotation-tjs](./test/programs/annotation-tjs)
252
253```ts
254// All of these formats are defined in this specification: http://json-schema.org/latest/json-schema-validation.html#rfc.section.8.3
255
256interface MyRef {}
257
258interface MyObject {
259 /**
260 * @TJS-format date-time
261 */
262 dateTime: string;
263
264 /**
265 * @TJS-format email
266 */
267 email: string;
268
269 /**
270 * @TJS-format hostname
271 */
272 hostname: string;
273
274 /**
275 * @TJS-format ipv4
276 */
277 ipv4: string;
278
279 /**
280 * @TJS-format ipv6
281 */
282 ipv6: string;
283
284 /**
285 * @TJS-format uri
286 */
287 uri: string;
288
289 /**
290 * @TJS-format uri-reference
291 */
292 uriReference: string;
293
294 /**
295 * @TJS-format uri-template
296 */
297 uriTemplate: string;
298
299 /**
300 * @TJS-format json-pointer
301 */
302 jsonPointer: string;
303
304 /**
305 * @TJS-pattern ^[a-zA-Z0-9]{4}-abc_123$
306 */
307 regexPattern: string;
308
309 /**
310 * @TJS-pattern ^[a-zA-Z0-9]{4}-abc_123$
311 */
312 regexPatternWithWhitespace: string;
313
314 /**
315 * @TJS-minimum 5
316 */
317 oneCharacter: number;
318
319 /**
320 * @TJS-examples ["foo", 1]
321 */
322 examples: string;
323
324 /**
325 * @TJS-hide
326 */
327 booleanAnnotationDefaultValue: string;
328
329 /**
330 * @TJS-hide true
331 */
332 booleanAnnotationWithTrue: string;
333
334 /**
335 * @TJS-hide false
336 */
337 booleanAnnotationWithFalse: string;
338
339 /**
340 * @TJS-ignore
341 */
342 complexWithRefIgnored: MyRef;
343}
344```
345
346
347## [any-unknown](./test/programs/any-unknown)
348
349```ts
350export interface MyObject {
351 a: any;
352 b: unknown;
353}
354```
355
356
357## [argument-id](./test/programs/argument-id)
358
359```ts
360interface MyObject {
361 someProp: string;
362 referenceType: ReferenceType;
363}
364
365interface ReferenceType {
366 reference: true;
367}
368```
369
370
371## [array-and-description](./test/programs/array-and-description)
372
373```ts
374export interface MyObject {
375 /**
376 * A name
377 */
378 name?: string;
379 description?: string;
380 test: any[];
381}
382```
383
384
385## [array-empty](./test/programs/array-empty)
386
387```ts
388type MyEmptyArray = [];
389```
390
391
392## [array-readonly](./test/programs/array-readonly)
393
394```ts
395export type MyReadOnlyArray = ReadonlyArray<number>;
396```
397
398
399## [array-types](./test/programs/array-types)
400
401```ts
402type MyArray = Array<string | number>;
403```
404
405
406## [builtin-names](./test/programs/builtin-names)
407
408```ts
409declare namespace Ext {
410 export class Array {
411 }
412
413 export class Foo {
414 bar: Ext.Array;
415 }
416}
417```
418
419
420## [class-extends](./test/programs/class-extends)
421
422```ts
423class Base {
424 propA:number;
425}
426
427class MyObject extends Base {
428 propB:number;
429}
430```
431
432
433## [class-single](./test/programs/class-single)
434
435```ts
436class MyObject {
437 constructor() {}
438 propA:number;
439 propB:number;
440 doNotInclude(): void {}
441}
442```
443
444
445## [comments](./test/programs/comments)
446
447```ts
448
449/**
450 * Description of Vector3D, a type alias to a array of integers with length 3
451 * If run without useTypeAliasRef, this comment should be ignored but
452 * the other annotations should be inherited
453 * @minItems 3
454 * @maxItems 3
455 */
456type Vector3D = number[];
457
458/**
459 * Description of MyObject, a top level object,
460 * which also has a comment that spans
461 * multiple lines
462 *
463 * @additionalProperties false
464 * @unsupportedAnnotationThatShouldBeIgnored
465 */
466interface MyObject {
467
468 /**
469 * Description of opacity, a field with min/max values
470 * @minimum 0
471 * @maximum 100
472 */
473 opacity: number;
474
475 /**
476 * Description of field position, of aliased type Vector3D, which should inherit its annotations
477 */
478 position: Vector3D;
479
480 /**
481 * Description of rotation, a field with an anonymous type
482 */
483 rotation: {
484 /**
485 * Description of the value yaw inside an anonymous type, with min/max annotations
486 * @minimum -90
487 * @maximum 90
488 */
489 yaw: number;
490 };
491}
492```
493
494
495## [comments-comment](./test/programs/comments-comment)
496
497```ts
498/**
499 * @$comment Object comment
500 */
501interface MyObject {
502 /**
503 * @$comment Property comment
504 */
505 text: string;
506}
507```
508
509
510## [comments-from-lib](./test/programs/comments-from-lib)
511
512```ts
513/**
514 * Use this comment
515 */
516export type MyObject = Pick<BigThing, "prop1">;
517
518/**
519 * Not this comment though
520 */
521interface BigThing {
522 prop1: string;
523 prop2: string;
524};
525```
526
527
528## [comments-imports](./test/programs/comments-imports)
529
530```ts
531/**
532 * Description of Color.
533 *
534 * @pattern ^[0-9a-f]{6}$
535 */
536export type Color = string;
537```
538
539
540## [comments-imports](./test/programs/comments-imports)
541
542```ts
543import { Color } from "./color";
544import { Text } from "./text";
545
546/** Description of MyObject */
547export interface MyObject {
548 /** Description of MyObject color property. */
549 color: Color;
550
551 /** Description of MyObject text property. */
552 text: Text;
553}
554```
555
556
557## [comments-imports](./test/programs/comments-imports)
558
559```ts
560import { Color } from "./color";
561
562/**
563 * Description of Text interface.
564 */
565export interface Text {
566 /** Description of text property. */
567 text: string;
568
569 /** Description of text color property. */
570 color: Color;
571}
572```
573
574
575## [comments-inline-tags](./test/programs/comments-inline-tags)
576
577```ts
578/**
579 * This is MyOtherObject
580 */
581interface MyOtherObject {
582 prop1: string;
583}
584
585/**
586 * This is MyObject. It extends {@link MyOtherObject} and {@link SomeOtherObject}.
587 */
588interface MyObject extends MyOtherObject {
589 prop2: string;
590}
591```
592
593
594## [comments-override](./test/programs/comments-override)
595
596```ts
597
598/**
599 * Type-level description
600 * @additionalProperties true
601 */
602export interface MySubObject {
603 value: string;
604}
605
606export interface MyObject {
607 list: MySubObject[];
608
609 sub1: MySubObject;
610
611 /**
612 * Property-level description
613 * @additionalProperties false
614 */
615 sub2: MySubObject;
616
617 /**
618 * Date property description
619 */
620 date: Date;
621}
622```
623
624
625## [const-as-enum](./test/programs/const-as-enum)
626
627```ts
628export interface MyObject {
629 reference: true;
630}
631```
632
633
634## [const-keyword](./test/programs/const-keyword)
635
636```ts
637const fn = <const T>(value: T) =>
638 ({ value });
639
640export type Object = ReturnType<typeof fn<"value">>;
641```
642
643
644## [custom-dates](./test/programs/custom-dates)
645
646```ts
647namespace foo {
648 export interface Date {
649 day?: number;
650 month?: number;
651 year?: number;
652 }
653
654 export interface Bar {
655 date: Date;
656 }
657}
658```
659
660
661## [dates](./test/programs/dates)
662
663```ts
664type DateAlias = Date;
665
666interface MyObject {
667 var1: Date;
668 var2: DateAlias;
669 /**
670 * @format date
671 */
672 var3: Date;
673 /**
674 * @format date
675 */
676 var4: DateAlias;
677}
678```
679
680
681## [default-properties](./test/programs/default-properties)
682
683```ts
684type Foo = "a" | "b" | "c" | boolean | number;
685
686class MyObject {
687 varBoolean: Foo = <any> false;
688 varInteger: Foo = <any> 123;
689 varString: Foo = <any> "123";
690}
691```
692
693
694## [enums-compiled-compute](./test/programs/enums-compiled-compute)
695
696```ts
697enum Enum {
698 X = 1 << 1,
699 Y = 1 << 2,
700 Z = X | Y,
701 A = 1,
702}
703```
704
705
706## [enums-mixed](./test/programs/enums-mixed)
707
708```ts
709enum Enum {
710 A, // = 0
711 B = 1,
712 C = true as any,
713 D = "str" as any,
714 E = null
715}
716
717interface MyObject {
718 foo: Enum;
719}
720```
721
722
723## [enums-number](./test/programs/enums-number)
724
725```ts
726enum Enum {
727 X = 1,
728 Y = 2
729}
730
731interface MyObject {
732 foo: Enum;
733}
734```
735
736
737## [enums-number-initialized](./test/programs/enums-number-initialized)
738
739```ts
740enum Enum {
741 X = 10,
742 Y,
743 Z,
744 A = 1,
745}
746```
747
748
749## [enums-string](./test/programs/enums-string)
750
751```ts
752enum Enum {
753 X = "x" as any,
754 Y = "y" as any,
755 Z = "123" as any
756}
757
758interface MyObject {
759 foo: Enum;
760}
761```
762
763
764## [enums-value-in-interface](./test/programs/enums-value-in-interface)
765
766```ts
767export enum A {
768 B,
769 C,
770 D,
771};
772
773export interface MyObject {
774 code: A.B;
775};
776```
777
778
779## [extra-properties](./test/programs/extra-properties)
780
781```ts
782export interface MyObject {
783 required: string;
784 optional?: number;
785 [name: string]: string|number;
786}
787```
788
789
790## [force-type](./test/programs/force-type)
791
792```ts
793/** @TJS-type number */
794export class Widget {}
795
796export interface MyObject {
797 name: string;
798
799 mainWidget: Widget;
800 otherWidgets: Widget[];
801}
802```
803
804
805## [force-type-imported](./test/programs/force-type-imported)
806
807```ts
808import { Widget } from "./widget";
809
810export interface MyObject {
811 name: string;
812
813 mainWidget: Widget;
814 otherWidgets: Widget[];
815}
816```
817
818
819## [force-type-imported](./test/programs/force-type-imported)
820
821```ts
822/** @TJS-type number */
823export class Widget {}
824```
825
826
827## [generate-all-types](./test/programs/generate-all-types)
828
829```ts
830
831
832interface MyInterface {
833
834}
835
836class MyObject {
837
838}
839
840enum MyEnum {
841 Value = 0
842}
843```
844
845
846## [generic-anonymous](./test/programs/generic-anonymous)
847
848```ts
849interface MyGeneric<A, B> {
850 a: A;
851 b: B;
852}
853
854export interface MyObject {
855 value1: MyGeneric<string, number>;
856 value2: MyGeneric<number, string>;
857}
858```
859
860
861## [generic-arrays](./test/programs/generic-arrays)
862
863```ts
864export interface MyObject {
865 numberArray: Array<number>;
866 stringArray: ReadonlyArray<string>;
867}
868```
869
870
871## [generic-hell](./test/programs/generic-hell)
872
873```ts
874export interface GenericA<A> {
875 a: A;
876}
877export interface B {
878 b: number;
879}
880export interface GenericC<C> {
881 c: C;
882}
883
884export type SomeAlias<T> = SomeGeneric<T, T>;
885export interface SomeGeneric<A, B> {
886 a: A;
887 b: B;
888 c: GenericA<A>;
889 d: GenericC<B>;
890}
891
892export interface MyObject extends GenericC<GenericC<GenericC<GenericA<string>>>>, B {
893 someGeneric: SomeGeneric<1, 2>;
894 someAlias: SomeAlias<"alias">;
895}
896```
897
898
899## [generic-multiargs](./test/programs/generic-multiargs)
900
901```ts
902export interface MyGeneric<A, B> {
903 a: A;
904 b: B;
905}
906
907export interface MyObject {
908 value1: MyGeneric<string, number>;
909 value2: MyGeneric<number, string>;
910}
911```
912
913
914## [generic-multiple](./test/programs/generic-multiple)
915
916```ts
917export interface MyGeneric<T> {
918 field: T;
919}
920
921export interface MyObject {
922 value1: MyGeneric<number>;
923 value2: MyGeneric<string>;
924}
925```
926
927
928## [generic-recursive](./test/programs/generic-recursive)
929
930```ts
931export interface MyGeneric<A, B> {
932 field: MyGeneric<B, A>;
933}
934
935export interface MyObject {
936 value: MyGeneric<string, number>;
937}
938```
939
940
941## [generic-simple](./test/programs/generic-simple)
942
943```ts
944export interface MyGeneric<T> {
945 field: T;
946}
947
948export interface MyObject {
949 value: MyGeneric<number>;
950}
951```
952
953
954## [ignored-required](./test/programs/ignored-required)
955
956```ts
957interface MyObject {
958 /**
959 * @ignore
960 */
961 ignored: boolean;
962
963 /**
964 * @ignore
965 */
966 ignoredOptional?: boolean;
967
968 required: boolean;
969 optional?: boolean;
970}
971```
972
973
974## [imports](./test/programs/imports)
975
976```ts
977
978// This file imports "MyInterface" from the other 2 files
979// while also declaring a MyInterface type
980
981import { MyInterface as module1_MyInterface } from "./module1";
982import * as module2 from "./module2";
983
984class MyInterface {
985 fieldInMain: number;
986}
987
988class MyObject {
989 a: MyInterface;
990 b: module1_MyInterface;
991 c: module2.MyInterface;
992}
993```
994
995
996## [imports](./test/programs/imports)
997
998```ts
999
1000export class MyInterface {
1001 fieldInModule1: string;
1002}
1003
1004```
1005
1006
1007## [imports](./test/programs/imports)
1008
1009```ts
1010
1011export class MyInterface {
1012 fieldInModule2: number;
1013}
1014
1015```
1016
1017
1018## [interface-extends](./test/programs/interface-extends)
1019
1020```ts
1021interface Base {
1022 propA: number;
1023}
1024
1025export interface MyObject extends Base {
1026 propB: number;
1027}
1028```
1029
1030
1031## [interface-extra-props](./test/programs/interface-extra-props)
1032
1033```ts
1034export interface MyObject {
1035 required: string;
1036 optional?: number;
1037 [name: string]: string|number;
1038}
1039```
1040
1041
1042## [interface-multi](./test/programs/interface-multi)
1043
1044```ts
1045interface MyObject {
1046 subA: MySubObject;
1047 subB: MySubObject;
1048}
1049interface MySubObject {
1050 propA: number;
1051 propB: number;
1052}
1053```
1054
1055
1056## [interface-recursion](./test/programs/interface-recursion)
1057
1058```ts
1059interface MyObject {
1060 propA: number;
1061 propB: MyObject;
1062}
1063```
1064
1065
1066## [interface-single](./test/programs/interface-single)
1067
1068```ts
1069export interface MyObject {
1070 propA: number;
1071 propB: number;
1072}
1073```
1074
1075
1076## [key-in-key-of-multi](./test/programs/key-in-key-of-multi)
1077
1078```ts
1079type Util = {
1080 utilKey1: {
1081 utilDeepKey11: string;
1082 utilDeepKey12: number;
1083 };
1084 utilKey2: {
1085 utilDeepKey21: boolean;
1086 utilDeepKey22: null;
1087 };
1088};
1089
1090export type Main = {
1091 [Key in keyof Util]: {
1092 [key: string]: Util[Key];
1093 };
1094};
1095```
1096
1097
1098## [key-in-key-of-multi-underscores](./test/programs/key-in-key-of-multi-underscores)
1099
1100```ts
1101type Util = {
1102 __2Underscores: {
1103 utilDeepKey2: string;
1104 };
1105 ___3Underscores: {
1106 utilDeepKey3: string;
1107 };
1108 ____4Underscores: {
1109 utilDeepKey4: string;
1110 };
1111};
1112
1113export type Main = {
1114 [Key in keyof Util]: {
1115 [key: string]: Util[Key];
1116 };
1117};
1118```
1119
1120
1121## [key-in-key-of-single](./test/programs/key-in-key-of-single)
1122
1123```ts
1124type Util = {
1125 utilKey: {
1126 utilDeepKey: string;
1127 };
1128};
1129
1130export type Main = {
1131 [Key in keyof Util]: {
1132 [key: string]: Util[Key];
1133 };
1134};
1135```
1136
1137
1138## [map-types](./test/programs/map-types)
1139
1140```ts
1141
1142interface MyType {}
1143
1144interface MyMap1 {
1145 [id: string]: MyType;
1146}
1147
1148/**
1149 * The additionalProperties annotation should be ignored
1150 * @additionalProperties false
1151 */
1152interface MyMap2 {
1153 [id: string]: (string | number);
1154}
1155
1156type MyMap3 = Readonly<MyMap2>;
1157
1158interface MyObject {
1159 map1: MyMap1;
1160 map2: MyMap2;
1161 map3: MyMap3;
1162}
1163```
1164
1165
1166## [module-interface-deep](./test/programs/module-interface-deep)
1167
1168```ts
1169module MyModule {
1170 export interface Def {
1171 nest: Def;
1172 prev: MyModule.Def;
1173 propA: SubModule.HelperA;
1174 propB: SubModule.HelperB;
1175 }
1176 export module SubModule {
1177 export interface HelperA {
1178 propA: number;
1179 propB: HelperB;
1180 }
1181 export interface HelperB {
1182 propA: SubModule.HelperA;
1183 propB: Def;
1184 }
1185 }
1186}
1187```
1188
1189
1190## [module-interface-single](./test/programs/module-interface-single)
1191
1192```ts
1193module MyModule {
1194 interface MyObject {
1195 propA: number;
1196 propB: number;
1197 }
1198}
1199```
1200
1201
1202## [namespace](./test/programs/namespace)
1203
1204```ts
1205export namespace Types {
1206 export const X: "x" = "x";
1207 export const Y: "y" = "y";
1208}
1209
1210export type Type = typeof Types.X | typeof Types.Y;
1211```
1212
1213
1214## [namespace-deep-1](./test/programs/namespace-deep-1)
1215
1216```ts
1217namespace RootNamespace {
1218 export interface Def {
1219 nest: Def;
1220 prev: RootNamespace.Def;
1221
1222 propA: SubNamespace.HelperA;
1223 propB: SubNamespace.HelperB;
1224 }
1225
1226 export namespace SubNamespace {
1227 export interface HelperA {
1228 propA: number;
1229 propB: HelperB;
1230 }
1231 export interface HelperB {
1232 propA: SubNamespace.HelperA;
1233 propB: Def;
1234 }
1235 }
1236}
1237```
1238
1239
1240## [namespace-deep-2](./test/programs/namespace-deep-2)
1241
1242```ts
1243namespace RootNamespace {
1244 export interface Def {
1245 nest: Def;
1246 prev: RootNamespace.Def;
1247
1248 propA: SubNamespace.HelperA;
1249 propB: SubNamespace.HelperB;
1250 }
1251
1252 export namespace SubNamespace {
1253 export interface HelperA {
1254 propA: number;
1255 propB: HelperB;
1256 }
1257 export interface HelperB {
1258 propA: SubNamespace.HelperA;
1259 propB: Def;
1260 }
1261 }
1262}
1263```
1264
1265
1266## [never](./test/programs/never)
1267
1268```ts
1269export interface Never {
1270 neverProp: never;
1271 propA: string;
1272}
1273```
1274
1275
1276## [no-ref](./test/programs/no-ref)
1277
1278```ts
1279type MySubType = {
1280 id: string;
1281};
1282
1283export type MyModule = {
1284 address: MySubType & { extraProp: number };
1285 address2: MySubType;
1286 address3: MySubType;
1287};
1288```
1289
1290
1291## [no-unrelated-definitions](./test/programs/no-unrelated-definitions)
1292
1293```ts
1294export interface MyObject {
1295 sub: SomeDefinition;
1296}
1297
1298interface SomeDefinition {
1299 is: string;
1300}
1301
1302export interface MyOtherObject {
1303 sub: SomeOtherDefinition;
1304}
1305
1306interface SomeOtherDefinition {
1307 is: string;
1308}
1309```
1310
1311
1312## [numeric-keys-and-others](./test/programs/numeric-keys-and-others)
1313
1314```ts
1315interface NumericKeysAndOthers {
1316 [key: number]: number;
1317 a: string;
1318 b: boolean;
1319}
1320```
1321
1322
1323## [object-numeric-index](./test/programs/object-numeric-index)
1324
1325```ts
1326interface IndexInterface {
1327 [index: number]: number;
1328}
1329```
1330
1331
1332## [object-numeric-index-as-property](./test/programs/object-numeric-index-as-property)
1333
1334```ts
1335interface Target {
1336 objAnonymous: {
1337 [index: number]: number;
1338 };
1339 objInterface: IndexInterface;
1340 indexInType: { [index in number]?: number };
1341 indexInInline: { [index in number]: number };
1342 indexInPartialType: IndexInPartial;
1343 indexInPartialInline: { [index in number]?: number };
1344}
1345interface IndexInterface {
1346 [index: number]: number;
1347}
1348
1349type IndexIn = { [index in number]: number };
1350type IndexInPartial = { [index in number]?: number };
1351```
1352
1353
1354## [optionals](./test/programs/optionals)
1355
1356```ts
1357interface MyObject {
1358 required:number;
1359 optional?:number;
1360}
1361```
1362
1363
1364## [optionals-derived](./test/programs/optionals-derived)
1365
1366```ts
1367interface MyBase {
1368 baseRequired : number;
1369 baseOptional?: number;
1370}
1371
1372interface MyDerived extends MyBase {
1373 derivedRequired : number;
1374 derivedOptional?: number;
1375}
1376```
1377
1378
1379## [private-members](./test/programs/private-members)
1380
1381```ts
1382export class MyObject {
1383 publicMember: string;
1384 private privateMember: string;
1385}
1386```
1387
1388
1389## [prop-override](./test/programs/prop-override)
1390
1391```ts
1392import type { ObjectId } from './third-party'
1393
1394export type MyObject = {
1395 /**
1396 * @TJS-type string
1397 * @description Overrides aliased type definition with this JSDoc if at least TJS-type annotation is present
1398 */
1399 _id: ObjectId
1400}
1401```
1402
1403
1404## [prop-override](./test/programs/prop-override)
1405
1406```ts
1407// cannot modify with JSDoc because third-party sources
1408export class ObjectId {}
1409```
1410
1411
1412## [satisfies-keyword](./test/programs/satisfies-keyword)
1413
1414```ts
1415interface Basic {
1416 a: string;
1417 b: number;
1418 c: boolean;
1419}
1420
1421const myObject = {
1422 a: "a" as const,
1423 b: 1 as const,
1424 c: true as const,
1425// tslint:disable-next-line:variable-name
1426} satisfies Basic;
1427
1428export type Specific = typeof myObject;
1429```
1430
1431
1432## [strict-null-checks](./test/programs/strict-null-checks)
1433
1434```ts
1435
1436class MyObject {
1437 val: number;
1438 valNullable: number | null;
1439 valUndef: number | undefined;
1440 valOpt?: number;
1441 valVoid: number | void;
1442
1443 valTrueOpt?: true;
1444 valTrueOrNull: true|null;
1445 valTrue: true|true; // twice to check that it will appear only once
1446}
1447```
1448
1449
1450## [string-literals](./test/programs/string-literals)
1451
1452```ts
1453type result = "ok" | "fail" | "abort" | "";
1454
1455class MyObject {
1456 foo: result;
1457 bar: result | string;
1458}
1459```
1460
1461
1462## [string-literals-inline](./test/programs/string-literals-inline)
1463
1464```ts
1465class MyObject {
1466 foo: "ok" | "fail" | "abort" | "";
1467 bar: "ok" | "fail" | "abort" | string;
1468}
1469```
1470
1471
1472## [string-template-literal](./test/programs/string-template-literal)
1473
1474```ts
1475interface MyObject {
1476 a: `@${string}`,
1477 b: `@${number}`,
1478 c: `@${bigint}`,
1479 d: `@${boolean}`,
1480 e: `@${undefined}`,
1481 f: `@${null}`,
1482 g: `${string}@`,
1483 h: `${number}@`,
1484 i: `${string}@${number}`,
1485 j: `${string}.${string}`,
1486}
1487```
1488
1489
1490## [symbol](./test/programs/symbol)
1491
1492```ts
1493export type MyObject = {
1494 a: symbol;
1495};
1496```
1497
1498
1499## [tsconfig](./test/programs/tsconfig)
1500
1501```ts
1502// This file is ignored.
1503
1504export interface Excluded {
1505 a: string;
1506}
1507```
1508
1509
1510## [tsconfig](./test/programs/tsconfig)
1511
1512```ts
1513// This file is included by tsconfig.json and --include.
1514
1515export interface IncludedAlways {
1516 a: string;
1517};
1518```
1519
1520
1521## [tsconfig](./test/programs/tsconfig)
1522
1523```ts
1524// This file is included by tsconfig.json.
1525
1526export interface IncludedOnlyByTsConfig {
1527 a: string;
1528};
1529```
1530
1531
1532## [type-alias-never](./test/programs/type-alias-never)
1533
1534```ts
1535export type MyNever = never;
1536```
1537
1538
1539## [type-alias-or](./test/programs/type-alias-or)
1540
1541```ts
1542interface A {}
1543interface B {}
1544
1545type C = A | B;
1546
1547interface MyObject {
1548 c: C;
1549}
1550```
1551
1552
1553## [type-alias-schema-override](./test/programs/type-alias-schema-override)
1554
1555```ts
1556interface All {}
1557
1558type Some = Partial<All>;
1559
1560interface MyObject {
1561 some?: Some;
1562}
1563```
1564
1565
1566## [type-alias-single](./test/programs/type-alias-single)
1567
1568```ts
1569type MyString = string;
1570```
1571
1572
1573## [type-alias-single-annotated](./test/programs/type-alias-single-annotated)
1574
1575```ts
1576/**
1577 * This is a description
1578 * @pattern ^mystring-[a-zA-Z0-9]+$
1579 * @minLength 10
1580 * @maxLength 24
1581 */
1582type MyString = string;
1583```
1584
1585
1586## [type-alias-undefined](./test/programs/type-alias-undefined)
1587
1588```ts
1589export type MyUndefined = undefined;
1590```
1591
1592
1593## [type-aliases](./test/programs/type-aliases)
1594
1595```ts
1596/**
1597 * My string
1598 */
1599type MyString = string;
1600
1601/**
1602 * My type alias
1603 */
1604type MyAlias = MySubObject;
1605
1606/**
1607 * My sub object
1608 */
1609interface MySubObject {
1610 propA: number;
1611 propB: number;
1612}
1613
1614/**
1615 * My Object
1616 */
1617interface MyObject {
1618 primitive: MyString;
1619 object: MySubObject;
1620 alias: MyAlias;
1621}
1622```
1623
1624
1625## [type-aliases-alias-ref](./test/programs/type-aliases-alias-ref)
1626
1627```ts
1628
1629interface MyObject {
1630 prop: number;
1631}
1632
1633type MyAlias = MyObject;
1634```
1635
1636
1637## [type-aliases-alias-ref-topref](./test/programs/type-aliases-alias-ref-topref)
1638
1639```ts
1640
1641interface MyObject {
1642 prop: number;
1643}
1644
1645type MyAlias = MyObject;
1646```
1647
1648
1649## [type-aliases-anonymous](./test/programs/type-aliases-anonymous)
1650
1651```ts
1652export type MyExportString = string;
1653type MyPrivateString = string;
1654
1655export interface MyObject {
1656 export: MyExportString;
1657 private: MyPrivateString;
1658}
1659```
1660
1661
1662## [type-aliases-fixed-size-array](./test/programs/type-aliases-fixed-size-array)
1663
1664```ts
1665type MyFixedSizeArray = [string, number];
1666
1667```
1668
1669
1670## [type-aliases-local-namespace](./test/programs/type-aliases-local-namespace)
1671
1672```ts
1673namespace A {
1674 export interface A { a: any; }
1675}
1676namespace B {
1677 export interface B { b: any; }
1678}
1679namespace C {
1680 import A = B.B;
1681 export interface C { c: A; }
1682}
1683namespace D {
1684 import A = C.C;
1685 export interface D { d: A; }
1686}
1687
1688export interface MyObject extends D.D {}
1689```
1690
1691
1692## [type-aliases-local-namsepace](./test/programs/type-aliases-local-namsepace)
1693
1694```ts
1695namespace A {
1696 export interface A {a: any;}
1697}
1698namespace B {
1699 export interface B {b: any;}
1700}
1701namespace C {
1702 import A = B.B;
1703 export interface C {c: A;}
1704}
1705namespace D {
1706 import A = C.C;
1707 export interface D {d: A;}
1708}
1709
1710interface MyObject extends D.D {}
1711```
1712
1713
1714## [type-aliases-mixed](./test/programs/type-aliases-mixed)
1715
1716```ts
1717export type MyString = string;
1718
1719export interface MySubObject {
1720 propA: number;
1721 propB: number;
1722}
1723
1724export interface MyObject {
1725 primitive: MyString;
1726 object: MySubObject;
1727}
1728```
1729
1730
1731## [type-aliases-multitype-array](./test/programs/type-aliases-multitype-array)
1732
1733```ts
1734
1735type BasicArray = (string | number)[];
1736
1737interface MyObject {
1738 array: BasicArray;
1739}
1740
1741type MyArray = (string | MyObject)[];
1742```
1743
1744
1745## [type-aliases-object](./test/programs/type-aliases-object)
1746
1747```ts
1748
1749export interface MyObject {
1750 number: number;
1751 string: string;
1752}
1753
1754export type MyAlias = MyObject;
1755```
1756
1757
1758## [type-aliases-partial](./test/programs/type-aliases-partial)
1759
1760```ts
1761export interface Foo {
1762 x: number;
1763 y: number;
1764}
1765
1766export interface Bar {
1767 a: number;
1768 b: number;
1769}
1770
1771export interface MyObject {
1772 foo: Partial<Foo>;
1773 bar: Partial<Bar>;
1774}
1775```
1776
1777
1778## [type-aliases-primitive](./test/programs/type-aliases-primitive)
1779
1780```ts
1781export type MyString = string;
1782```
1783
1784
1785## [type-aliases-recursive-alias-topref](./test/programs/type-aliases-recursive-alias-topref)
1786
1787```ts
1788
1789interface MyObject {
1790 alias: MyAlias;
1791 self: MyObject;
1792}
1793
1794type MyAlias = MyObject;
1795```
1796
1797
1798## [type-aliases-recursive-anonymous](./test/programs/type-aliases-recursive-anonymous)
1799
1800```ts
1801interface MyObject {
1802 alias: MyAlias;
1803 self: MyObject;
1804}
1805
1806export type MyAlias = MyObject;
1807```
1808
1809
1810## [type-aliases-recursive-export](./test/programs/type-aliases-recursive-export)
1811
1812```ts
1813export interface MyObject {
1814 alias: MyAlias;
1815 self: MyObject;
1816}
1817
1818export type MyAlias = MyObject;
1819```
1820
1821
1822## [type-aliases-recursive-object-topref](./test/programs/type-aliases-recursive-object-topref)
1823
1824```ts
1825
1826interface MyObject {
1827 alias: MyAlias;
1828 self: MyObject;
1829}
1830
1831type MyAlias = MyObject;
1832```
1833
1834
1835## [type-aliases-tuple](./test/programs/type-aliases-tuple)
1836
1837```ts
1838export type MyTuple = [string, number];
1839```
1840
1841
1842## [type-aliases-tuple-of-variable-length](./test/programs/type-aliases-tuple-of-variable-length)
1843
1844```ts
1845export type MyTuple = [string, number, boolean?];
1846```
1847
1848
1849## [type-aliases-tuple-with-rest-element](./test/programs/type-aliases-tuple-with-rest-element)
1850
1851```ts
1852export type MyTuple = [string, ...number[]];
1853```
1854
1855
1856## [type-aliases-union](./test/programs/type-aliases-union)
1857
1858```ts
1859
1860type BasicArray = (string | number)[];
1861
1862export interface MyObject {
1863 array: BasicArray;
1864}
1865
1866export type MyUnion = (string | MyObject)[];
1867```
1868
1869
1870## [type-aliases-union-namespace](./test/programs/type-aliases-union-namespace)
1871
1872```ts
1873
1874export namespace Cardinal {
1875 export const NORTH: "north" = "north";
1876 export const SOUTH: "south" = "south";
1877 export const EAST: "east" = "east";
1878 export const WEST: "west" = "west";
1879}
1880
1881export type Cardinal = typeof Cardinal.NORTH | typeof Cardinal.SOUTH | typeof Cardinal.EAST | typeof Cardinal.WEST;
1882
1883export interface MyModel {
1884 direction: Cardinal;
1885}
1886```
1887
1888
1889## [type-anonymous](./test/programs/type-anonymous)
1890
1891```ts
1892 interface MyObject {
1893 FieldWithAnonType: {
1894 SubfieldA: number;
1895 SubfieldB: (string | number);
1896 SubfieldC: {
1897 SubsubfieldA: number[];
1898 }
1899 };
1900}
1901```
1902
1903
1904## [type-default-number-as-integer](./test/programs/type-default-number-as-integer)
1905
1906```ts
1907interface MyObject {
1908 as_integer: number;
1909
1910 /** @TJS-type number */
1911 as_number: number;
1912}
1913```
1914
1915
1916## [type-function](./test/programs/type-function)
1917
1918```ts
1919interface MyObject {
1920 myFunction: Function;
1921}
1922```
1923
1924
1925## [type-globalThis](./test/programs/type-globalThis)
1926
1927```ts
1928export type Test = typeof globalThis;
1929```
1930
1931
1932## [type-intersection](./test/programs/type-intersection)
1933
1934```ts
1935interface Type1 {
1936 value1: string;
1937 value2: number;
1938}
1939interface Type2 {
1940 value2: number;
1941 value3: boolean;
1942}
1943
1944interface MyObject {
1945 value: Type1 & Type2;
1946}
1947```
1948
1949
1950## [type-intersection-recursive](./test/programs/type-intersection-recursive)
1951
1952```ts
1953interface ChildFoo {
1954}
1955
1956interface Foo {
1957 readonly childFoos: Foo | ChildFoo;
1958}
1959```
1960
1961
1962## [type-intersection-recursive-no-additional](./test/programs/type-intersection-recursive-no-additional)
1963
1964```ts
1965type MyRecursiveNode = {
1966 next?: MyNode;
1967}
1968
1969type MyNode = {
1970 val: string;
1971} & MyRecursiveNode;
1972
1973type MyLinkedList = MyNode;
1974```
1975
1976
1977## [type-literals](./test/programs/type-literals)
1978
1979```ts
1980type MyObject = {
1981 param1: "1" | "2" | "3";
1982 param2: "1" | "2" | 3 | true;
1983 /** @enum {string} */
1984 param3: "1" | "2" | "3";
1985 /** @enum {unknown} */
1986 param4: "1" | "2" | 3 | true;
1987};
1988```
1989
1990
1991## [type-mapped-types](./test/programs/type-mapped-types)
1992
1993```ts
1994type Keys = "str1" | "str2";
1995
1996type MyMappedType = {
1997 [key in Keys]: string;
1998};
1999```
2000
2001
2002## [type-no-aliases-recursive-topref](./test/programs/type-no-aliases-recursive-topref)
2003
2004```ts
2005
2006interface MyObject {
2007 alias: MyAlias;
2008 self: MyObject;
2009}
2010
2011type MyAlias = MyObject;
2012```
2013
2014
2015## [type-nullable](./test/programs/type-nullable)
2016
2017```ts
2018
2019/** @nullable */
2020type MyType1 = string;
2021
2022/** @nullable */
2023type MyType2 = string | number;
2024
2025/** @nullable */
2026type MyType3 = string | number[];
2027
2028/** @nullable */
2029type MyType4 = number[];
2030
2031type Ref = { foo: number };
2032
2033/** @nullable */
2034type MyType5 = Ref;
2035
2036interface MyType6 {};
2037
2038interface MyObject {
2039 var1: MyType1;
2040 var2: MyType2;
2041 var3: MyType3;
2042 var4: MyType4;
2043 var5: MyType5;
2044
2045 /**
2046 * @nullable
2047 */
2048 var6: MyType6;
2049 var7: MyType6;
2050}
2051```
2052
2053
2054## [type-primitives](./test/programs/type-primitives)
2055
2056```ts
2057/* tslint:disable:no-inferrable-types */
2058
2059// Special type, should not appear in the schema
2060type integer = number;
2061
2062class MyObject {
2063
2064 boolean1: boolean = true;
2065
2066 number1: number = 1;
2067
2068 /** @TJS-type integer */
2069 integer1: number = 1;
2070 integer2: integer = 1;
2071
2072 string1: string = "defaultValue";
2073
2074 array1: Array<any> = null;
2075 array2: Array<number> = null;
2076
2077 object1: any = null;
2078 object2: {} = null;
2079 object3: object = null;
2080
2081}
2082```
2083
2084
2085## [type-recursive](./test/programs/type-recursive)
2086
2087```ts
2088/**
2089 * A recursive type
2090 */
2091export type TestChildren = TestChild | Array<TestChild | TestChildren>;
2092
2093interface TestChild {
2094 type: string;
2095}
2096```
2097
2098
2099## [type-union](./test/programs/type-union)
2100
2101```ts
2102
2103// Simple union (generates "type": [...])
2104type MyType1 = string | number;
2105
2106// Non-simple union (generates a "oneOf"/"anyOf")
2107type MyType2 = string | number[];
2108
2109interface MyObject {
2110 var1: MyType1;
2111 var2: MyType2;
2112}
2113```
2114
2115
2116## [type-union-strict-null-keep-description](./test/programs/type-union-strict-null-keep-description)
2117
2118```ts
2119/**
2120 * Description of InnerObject.
2121 */
2122type InnerObject = {
2123 /**
2124 * Description of foo.
2125 */
2126 foo: string;
2127};
2128
2129/**
2130 * Description of MyObject.
2131 */
2132type MyObject = {
2133
2134 inner1?: InnerObject;
2135
2136 /**
2137 * Override description.
2138 */
2139 inner2?: InnerObject;
2140};
2141```
2142
2143
2144## [type-union-tagged](./test/programs/type-union-tagged)
2145
2146```ts
2147
2148interface Square {
2149 kind: "square";
2150 size: number;
2151}
2152
2153interface Rectangle {
2154 kind: "rectangle";
2155 width: number;
2156 height: number;
2157}
2158
2159interface Circle {
2160 kind: "circle";
2161 radius: number;
2162}
2163
2164type Shape = Square | Rectangle | Circle;
2165```
2166
2167
2168## [typeof-keyword](./test/programs/typeof-keyword)
2169
2170```ts
2171interface MyObject {
2172 foo: () => string;
2173}
2174```
2175
2176
2177## [undefined-property](./test/programs/undefined-property)
2178
2179```ts
2180export type MyObject = {
2181 a: string;
2182 b: undefined;
2183};
2184```
2185
2186
2187## [unique-names](./test/programs/unique-names)
2188
2189```ts
2190import "./other";
2191
2192class MyObject {
2193 is: "MyObject_1";
2194}
2195```
2196
2197
2198## [unique-names](./test/programs/unique-names)
2199
2200```ts
2201class MyObject {
2202 is: "MyObject_2";
2203}
2204```
2205
2206
2207## [unique-names-multiple-subdefinitions](./test/programs/unique-names-multiple-subdefinitions)
2208
2209```ts
2210import "./other";
2211
2212class SubObject {
2213 is: "SubObject_1";
2214}
2215
2216class MyObject {
2217 sub: SubObject;
2218}
2219```
2220
2221
2222## [unique-names-multiple-subdefinitions](./test/programs/unique-names-multiple-subdefinitions)
2223
2224```ts
2225class SubObject {
2226 is: "SubObject_2";
2227}
2228```
2229
2230
2231## [user-symbols](./test/programs/user-symbols)
2232
2233```ts
2234export interface Context {
2235 ip: string;
2236}
2237```
2238
2239
2240## [user-validation-keywords](./test/programs/user-validation-keywords)
2241
2242```ts
2243export interface MyObject {
2244 /**
2245 * Must be 'first' or 'last'
2246 *
2247 * @minLength 1
2248 * @chance {
2249 * "pickone": [ [ "first", "last" ] ]
2250 * }
2251 * @ignoreThis 2
2252 * @important
2253 */
2254 name: string;
2255}
2256```
2257
2258