-
Notifications
You must be signed in to change notification settings - Fork 28
/
type.ts
376 lines (344 loc) · 15.8 KB
/
type.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
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
import * as ts from 'typescript';
import {
isTypeParameter,
isUnionType,
isIntersectionType,
isLiteralType,
isObjectType,
isTupleTypeReference,
isUniqueESSymbolType,
} from '../typeguard/type';
import {
isTypeFlagSet,
isReadonlyAssignmentDeclaration,
isInConstContext,
isObjectFlagSet,
isSymbolFlagSet,
isModifierFlagSet,
isNodeFlagSet,
isNumericPropertyName,
PropertyName,
getBaseOfClassLikeExpression,
getSingleLateBoundPropertyNameOfPropertyName,
hasModifier,
getChildOfKind,
} from './util';
import {
isPropertyAssignment,
isVariableDeclaration,
isCallExpression,
isShorthandPropertyAssignment,
isEnumMember,
isClassLikeDeclaration,
isInterfaceDeclaration,
isSourceFile,
} from '../typeguard/node';
export function isEmptyObjectType(type: ts.Type): type is ts.ObjectType {
if (isObjectType(type) &&
type.objectFlags & ts.ObjectFlags.Anonymous &&
type.getProperties().length === 0 &&
type.getCallSignatures().length === 0 &&
type.getConstructSignatures().length === 0 &&
type.getStringIndexType() === undefined &&
type.getNumberIndexType() === undefined) {
const baseTypes = type.getBaseTypes();
return baseTypes === undefined || baseTypes.every(isEmptyObjectType);
}
return false;
}
export function removeOptionalityFromType(checker: ts.TypeChecker, type: ts.Type): ts.Type {
if (!containsTypeWithFlag(type, ts.TypeFlags.Undefined))
return type;
const allowsNull = containsTypeWithFlag(type, ts.TypeFlags.Null);
type = checker.getNonNullableType(type);
return allowsNull ? checker.getNullableType(type, ts.TypeFlags.Null) : type;
}
function containsTypeWithFlag(type: ts.Type, flag: ts.TypeFlags): boolean {
for (const t of unionTypeParts(type))
if (isTypeFlagSet(t, flag))
return true;
return false;
}
export function removeOptionalChainingUndefinedMarkerType(checker: ts.TypeChecker, type: ts.Type): ts.Type {
if (!isUnionType(type))
return isOptionalChainingUndefinedMarkerType(checker, type) ? type.getNonNullableType() : type;
let flags: ts.TypeFlags = 0;
let containsUndefinedMarker = false;
for (const t of type.types) {
if (isOptionalChainingUndefinedMarkerType(checker, t)) {
containsUndefinedMarker = true;
} else {
flags |= t.flags;
}
}
return containsUndefinedMarker
? checker.getNullableType(type.getNonNullableType(), flags)
: type;
}
export function isOptionalChainingUndefinedMarkerType(checker: ts.TypeChecker, t: ts.Type) {
return isTypeFlagSet(t, ts.TypeFlags.Undefined) && checker.getNullableType(t.getNonNullableType(), ts.TypeFlags.Undefined) !== t;
}
export function isTypeAssignableToNumber(checker: ts.TypeChecker, type: ts.Type): boolean {
return isTypeAssignableTo(checker, type, ts.TypeFlags.NumberLike);
}
export function isTypeAssignableToString(checker: ts.TypeChecker, type: ts.Type): boolean {
return isTypeAssignableTo(checker, type, ts.TypeFlags.StringLike);
}
function isTypeAssignableTo(checker: ts.TypeChecker, type: ts.Type, flags: ts.TypeFlags) {
flags |= ts.TypeFlags.Any;
let typeParametersSeen: Set<ts.Type> | undefined;
return (function check(t): boolean {
if (isTypeParameter(t) && t.symbol !== undefined && t.symbol.declarations !== undefined) {
if (typeParametersSeen === undefined) {
typeParametersSeen = new Set([t]);
} else if (!typeParametersSeen.has(t)) {
typeParametersSeen.add(t);
} else {
return false;
}
const declaration = <ts.TypeParameterDeclaration>t.symbol.declarations[0];
if (declaration.constraint === undefined)
return true; // TODO really?
return check(checker.getTypeFromTypeNode(declaration.constraint));
}
if (isUnionType(t))
return t.types.every(check);
if (isIntersectionType(t))
return t.types.some(check);
return isTypeFlagSet(t, flags);
})(type);
}
export function getCallSignaturesOfType(type: ts.Type): ReadonlyArray<ts.Signature> {
if (isUnionType(type)) {
const signatures = [];
for (const t of type.types)
signatures.push(...getCallSignaturesOfType(t));
return signatures;
}
if (isIntersectionType(type)) {
let signatures: ReadonlyArray<ts.Signature> | undefined;
for (const t of type.types) {
const sig = getCallSignaturesOfType(t);
if (sig.length !== 0) {
if (signatures !== undefined)
return []; // if more than one type of the intersection has call signatures, none of them is useful for inference
signatures = sig;
}
}
return signatures === undefined ? [] : signatures;
}
return type.getCallSignatures();
}
/** Returns all types of a union type or an array containing `type` itself if it's no union type. */
export function unionTypeParts(type: ts.Type): ts.Type[] {
return isUnionType(type) ? type.types : [type];
}
/** Returns all types of a intersection type or an array containing `type` itself if it's no intersection type. */
export function intersectionTypeParts(type: ts.Type): ts.Type[] {
return isIntersectionType(type) ? type.types : [type];
}
export function someTypePart(type: ts.Type, predicate: (t: ts.Type) => t is ts.UnionOrIntersectionType, cb: (t: ts.Type) => boolean) {
return predicate(type) ? type.types.some(cb) : cb(type);
}
/** Determines if a type thenable and can be used with `await`. */
export function isThenableType(checker: ts.TypeChecker, node: ts.Node, type: ts.Type): boolean;
/** Determines if a type thenable and can be used with `await`. */
export function isThenableType(checker: ts.TypeChecker, node: ts.Expression, type?: ts.Type): boolean;
export function isThenableType(checker: ts.TypeChecker, node: ts.Node, type = checker.getTypeAtLocation(node)!): boolean {
for (const ty of unionTypeParts(checker.getApparentType(type))) {
const then = ty.getProperty('then');
if (then === undefined)
continue;
const thenType = checker.getTypeOfSymbolAtLocation(then, node);
for (const t of unionTypeParts(thenType))
for (const signature of t.getCallSignatures())
if (signature.parameters.length !== 0 && isCallback(checker, signature.parameters[0], node))
return true;
}
return false;
}
function isCallback(checker: ts.TypeChecker, param: ts.Symbol, node: ts.Node): boolean {
let type: ts.Type | undefined = checker.getApparentType(checker.getTypeOfSymbolAtLocation(param, node));
if ((<ts.ParameterDeclaration>param.valueDeclaration).dotDotDotToken) {
// unwrap array type of rest parameter
type = type.getNumberIndexType();
if (type === undefined)
return false;
}
for (const t of unionTypeParts(type))
if (t.getCallSignatures().length !== 0)
return true;
return false;
}
/** Determine if a type is definitely falsy. This function doesn't unwrap union types. */
export function isFalsyType(type: ts.Type): boolean {
if (type.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null | ts.TypeFlags.Void))
return true;
if (isLiteralType(type))
return !type.value;
return isBooleanLiteralType(type, false);
}
/** Determines whether the given type is a boolean literal type and matches the given boolean literal (true or false). */
export function isBooleanLiteralType(type: ts.Type, literal: boolean) {
return isTypeFlagSet(type, ts.TypeFlags.BooleanLiteral) &&
(<{intrinsicName: string}><{}>type).intrinsicName === (literal ? 'true' : 'false');
}
export function getPropertyOfType(type: ts.Type, name: ts.__String) {
if (!(<string>name).startsWith('__'))
return type.getProperty(<string>name);
return type.getProperties().find((s) => s.escapedName === name);
}
export function getWellKnownSymbolPropertyOfType(type: ts.Type, wellKnownSymbolName: string, checker: ts.TypeChecker) {
const prefix = '__@' + wellKnownSymbolName;
for (const prop of type.getProperties()) {
if (!prop.name.startsWith(prefix))
continue;
const globalSymbol = checker.getApparentType(
checker.getTypeAtLocation((<ts.ComputedPropertyName>(<ts.NamedDeclaration>prop.valueDeclaration).name).expression),
).symbol;
if (prop.escapedName === getPropertyNameOfWellKnownSymbol(checker, globalSymbol, wellKnownSymbolName))
return prop;
}
return;
}
function getPropertyNameOfWellKnownSymbol(checker: ts.TypeChecker, symbolConstructor: ts.Symbol | undefined, symbolName: string) {
const knownSymbol = symbolConstructor &&
checker.getTypeOfSymbolAtLocation(symbolConstructor, symbolConstructor.valueDeclaration).getProperty(symbolName);
const knownSymbolType = knownSymbol && checker.getTypeOfSymbolAtLocation(knownSymbol, knownSymbol.valueDeclaration);
if (knownSymbolType && isUniqueESSymbolType(knownSymbolType))
return knownSymbolType.escapedName;
return <ts.__String>('__@' + symbolName);
}
/** Determines if writing to a certain property of a given type is allowed. */
export function isPropertyReadonlyInType(type: ts.Type, name: ts.__String, checker: ts.TypeChecker): boolean {
let seenProperty = false;
let seenReadonlySignature = false;
for (const t of unionTypeParts(type)) {
if (getPropertyOfType(t, name) === undefined) {
// property is not present in this part of the union -> check for readonly index signature
const index = (isNumericPropertyName(name) ? checker.getIndexInfoOfType(t, ts.IndexKind.Number) : undefined) ||
checker.getIndexInfoOfType(t, ts.IndexKind.String);
if (index !== undefined && index.isReadonly) {
if (seenProperty)
return true;
seenReadonlySignature = true;
}
} else if (seenReadonlySignature || isReadonlyPropertyIntersection(t, name, checker)) {
return true;
} else {
seenProperty = true;
}
}
return false;
}
function isReadonlyPropertyIntersection(type: ts.Type, name: ts.__String, checker: ts.TypeChecker) {
return someTypePart(type, isIntersectionType, (t) => {
const prop = getPropertyOfType(t, name);
if (prop === undefined)
return false;
if (prop.flags & ts.SymbolFlags.Transient) {
if (/^(?:[1-9]\d*|0)$/.test(<string>name) && isTupleTypeReference(t))
return t.target.readonly;
switch (isReadonlyPropertyFromMappedType(t, name, checker)) {
case true:
return true;
case false:
return false;
default:
// `undefined` falls through
}
}
return (
// members of namespace import
isSymbolFlagSet(prop, ts.SymbolFlags.ValueModule) ||
// we unwrapped every mapped type, now we can check the actual declarations
symbolHasReadonlyDeclaration(prop, checker)
);
});
}
function isReadonlyPropertyFromMappedType(type: ts.Type, name: ts.__String, checker: ts.TypeChecker): boolean | undefined {
if (!isObjectType(type) || !isObjectFlagSet(type, ts.ObjectFlags.Mapped))
return;
const declaration = <ts.MappedTypeNode>type.symbol!.declarations![0];
// well-known symbols are not affected by mapped types
if (declaration.readonlyToken !== undefined && !/^__@[^@]+$/.test(<string>name))
return declaration.readonlyToken.kind !== ts.SyntaxKind.MinusToken;
return isPropertyReadonlyInType((<{modifiersType: ts.Type}><unknown>type).modifiersType, name, checker);
}
export function symbolHasReadonlyDeclaration(symbol: ts.Symbol, checker: ts.TypeChecker) {
return (symbol.flags & ts.SymbolFlags.Accessor) === ts.SymbolFlags.GetAccessor ||
symbol.declarations !== undefined &&
symbol.declarations.some((node) =>
isModifierFlagSet(node, ts.ModifierFlags.Readonly) ||
isVariableDeclaration(node) && isNodeFlagSet(node.parent!, ts.NodeFlags.Const) ||
isCallExpression(node) && isReadonlyAssignmentDeclaration(node, checker) ||
isEnumMember(node) ||
(isPropertyAssignment(node) || isShorthandPropertyAssignment(node)) && isInConstContext(node.parent!),
);
}
/** Returns the the literal name or unique symbol name from a given type. Doesn't unwrap union types. */
export function getPropertyNameFromType(type: ts.Type): PropertyName | undefined {
// string or number literal. bigint is intentionally excluded
if (type.flags & (ts.TypeFlags.StringLiteral | ts.TypeFlags.NumberLiteral)) {
const value = String((<ts.StringLiteralType | ts.NumberLiteralType>type).value);
return {displayName: value, symbolName: ts.escapeLeadingUnderscores(value)};
}
if (isUniqueESSymbolType(type))
return {
displayName: `[${type.symbol
? `${isKnownSymbol(type.symbol) ? 'Symbol.' : ''}${type.symbol.name}`
: (<string>type.escapedName).replace(/^__@|@\d+$/g, '')
}]`,
symbolName: type.escapedName,
};
}
function isKnownSymbol(symbol: ts.Symbol): boolean {
return isSymbolFlagSet(symbol, ts.SymbolFlags.Property) &&
symbol.valueDeclaration !== undefined &&
isInterfaceDeclaration(symbol.valueDeclaration.parent) &&
symbol.valueDeclaration.parent.name.text === 'SymbolConstructor' &&
isGlobalDeclaration(symbol.valueDeclaration.parent);
}
function isGlobalDeclaration(node: ts.DeclarationStatement): boolean {
return isNodeFlagSet(node.parent!, ts.NodeFlags.GlobalAugmentation) || isSourceFile(node.parent) && !ts.isExternalModule(node.parent);
}
export function getSymbolOfClassLikeDeclaration(node: ts.ClassLikeDeclaration, checker: ts.TypeChecker) {
return checker.getSymbolAtLocation(node.name ?? getChildOfKind(node, ts.SyntaxKind.ClassKeyword)!)!;
}
export function getConstructorTypeOfClassLikeDeclaration(node: ts.ClassLikeDeclaration, checker: ts.TypeChecker) {
return node.kind === ts.SyntaxKind.ClassExpression
? checker.getTypeAtLocation(node)
: checker.getTypeOfSymbolAtLocation(getSymbolOfClassLikeDeclaration(node, checker), node);
}
export function getInstanceTypeOfClassLikeDeclaration(node: ts.ClassLikeDeclaration, checker: ts.TypeChecker) {
return node.kind === ts.SyntaxKind.ClassDeclaration
? checker.getTypeAtLocation(node)
: checker.getDeclaredTypeOfSymbol(getSymbolOfClassLikeDeclaration(node, checker));
}
export function getIteratorYieldResultFromIteratorResult(type: ts.Type, node: ts.Node, checker: ts.TypeChecker): ts.Type {
return isUnionType(type) && type.types.find((t) => {
const done = t.getProperty('done');
return done !== undefined &&
isBooleanLiteralType(removeOptionalityFromType(checker, checker.getTypeOfSymbolAtLocation(done, node)), false);
}) || type;
}
/** Lookup the declaration of a class member in the super class. */
export function getBaseClassMemberOfClassElement(
node: ts.PropertyDeclaration | ts.MethodDeclaration | ts.AccessorDeclaration,
checker: ts.TypeChecker,
): ts.Symbol | undefined {
if (!isClassLikeDeclaration(node.parent!))
return;
const base = getBaseOfClassLikeExpression(node.parent);
if (base === undefined)
return;
const name = getSingleLateBoundPropertyNameOfPropertyName(node.name, checker);
if (name === undefined)
return;
const baseType = checker.getTypeAtLocation(
hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword)
? base.expression
: base,
);
return getPropertyOfType(baseType, name.symbolName);
}