/
MappedTypeNodeParser.ts
182 lines (164 loc) · 7.59 KB
/
MappedTypeNodeParser.ts
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
import ts from "typescript";
import { LogicError } from "../Error/LogicError.js";
import { Context, NodeParser } from "../NodeParser.js";
import { SubNodeParser } from "../SubNodeParser.js";
import { AnnotatedType } from "../Type/AnnotatedType.js";
import { ArrayType } from "../Type/ArrayType.js";
import { BaseType } from "../Type/BaseType.js";
import { DefinitionType } from "../Type/DefinitionType.js";
import { EnumType, EnumValue } from "../Type/EnumType.js";
import { LiteralType } from "../Type/LiteralType.js";
import { NeverType } from "../Type/NeverType.js";
import { NumberType } from "../Type/NumberType.js";
import { ObjectProperty, ObjectType } from "../Type/ObjectType.js";
import { StringType } from "../Type/StringType.js";
import { SymbolType } from "../Type/SymbolType.js";
import { UnionType } from "../Type/UnionType.js";
import { derefAnnotatedType, derefType } from "../Utils/derefType.js";
import { getKey } from "../Utils/nodeKey.js";
import { preserveAnnotation } from "../Utils/preserveAnnotation.js";
import { removeUndefined } from "../Utils/removeUndefined.js";
export class MappedTypeNodeParser implements SubNodeParser {
public constructor(
protected childNodeParser: NodeParser,
protected readonly additionalProperties: boolean,
) {}
public supportsNode(node: ts.MappedTypeNode): boolean {
return node.kind === ts.SyntaxKind.MappedType;
}
public createType(node: ts.MappedTypeNode, context: Context): BaseType {
const constraintType = this.childNodeParser.createType(node.typeParameter.constraint!, context);
const keyListType = derefType(constraintType);
const id = `indexed-type-${getKey(node, context)}`;
if (keyListType instanceof UnionType) {
// Key type resolves to a set of known properties
return new ObjectType(
id,
[],
this.getProperties(node, keyListType, context),
this.getAdditionalProperties(node, keyListType, context),
);
} else if (keyListType instanceof LiteralType) {
// Key type resolves to single known property
return new ObjectType(id, [], this.getProperties(node, new UnionType([keyListType]), context), false);
} else if (
keyListType instanceof StringType ||
keyListType instanceof NumberType ||
keyListType instanceof SymbolType
) {
if (constraintType?.getId() === "number") {
const type = this.childNodeParser.createType(
node.type!,
this.createSubContext(node, keyListType, context),
);
return type instanceof NeverType ? new NeverType() : new ArrayType(type);
}
// Key type widens to `string`
const type = this.childNodeParser.createType(node.type!, context);
// const resultType = type instanceof NeverType ? new NeverType() : new ObjectType(id, [], [], type);
const resultType = new ObjectType(id, [], [], type);
if (resultType) {
let annotations;
if (constraintType instanceof AnnotatedType) {
annotations = constraintType.getAnnotations();
} else if (constraintType instanceof DefinitionType) {
const childType = constraintType.getType();
if (childType instanceof AnnotatedType) {
annotations = childType.getAnnotations();
}
}
if (annotations) {
return new AnnotatedType(resultType, { propertyNames: annotations }, false);
}
}
return resultType;
} else if (keyListType instanceof EnumType) {
return new ObjectType(id, [], this.getValues(node, keyListType, context), false);
} else if (keyListType instanceof NeverType) {
return new ObjectType(id, [], [], false);
} else {
throw new LogicError(
`Unexpected key type "${
constraintType ? constraintType.getId() : constraintType
}" for type "${node.getText()}" (expected "UnionType" or "StringType")`,
);
}
}
protected mapKey(node: ts.MappedTypeNode, rawKey: LiteralType, context: Context): BaseType {
if (!node.nameType) {
return rawKey;
}
const key = derefType(
this.childNodeParser.createType(node.nameType, this.createSubContext(node, rawKey, context)),
);
return key;
}
protected getProperties(node: ts.MappedTypeNode, keyListType: UnionType, context: Context): ObjectProperty[] {
return keyListType
.getTypes()
.filter((type): type is LiteralType => type instanceof LiteralType)
.map((type) => [type, this.mapKey(node, type, context)])
.filter((value): value is [LiteralType, LiteralType] => value[1] instanceof LiteralType)
.reduce((result: ObjectProperty[], [key, mappedKey]: [LiteralType, LiteralType]) => {
const propertyType = this.childNodeParser.createType(
node.type!,
this.createSubContext(node, key, context),
);
let newType = derefAnnotatedType(propertyType);
let hasUndefined = false;
if (newType instanceof UnionType) {
const { newType: newType_, numRemoved } = removeUndefined(newType);
hasUndefined = numRemoved > 0;
newType = newType_;
}
const objectProperty = new ObjectProperty(
mappedKey.getValue().toString(),
preserveAnnotation(propertyType, newType),
!node.questionToken && !hasUndefined,
);
result.push(objectProperty);
return result;
}, []);
}
protected getValues(node: ts.MappedTypeNode, keyListType: EnumType, context: Context): ObjectProperty[] {
return keyListType
.getValues()
.filter((value: EnumValue) => value != null)
.map((value: EnumValue) => {
const type = this.childNodeParser.createType(
node.type!,
this.createSubContext(node, new LiteralType(value!), context),
);
return new ObjectProperty(value!.toString(), type, !node.questionToken);
});
}
protected getAdditionalProperties(
node: ts.MappedTypeNode,
keyListType: UnionType,
context: Context,
): BaseType | boolean {
const key = keyListType.getTypes().filter((type) => !(type instanceof LiteralType))[0];
if (key) {
return (
this.childNodeParser.createType(node.type!, this.createSubContext(node, key, context)) ??
this.additionalProperties
);
} else {
return this.additionalProperties;
}
}
protected createSubContext(
node: ts.MappedTypeNode,
key: LiteralType | StringType | NumberType,
parentContext: Context,
): Context {
const subContext = new Context(node);
parentContext.getParameters().forEach((parentParameter) => {
subContext.pushParameter(parentParameter);
subContext.pushArgument(parentContext.getArgument(parentParameter));
});
subContext.pushParameter(node.typeParameter.name.text);
subContext.pushArgument(key);
return subContext;
}
}