/
predicates.ts
189 lines (168 loc) · 4.63 KB
/
predicates.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
import debug from 'debug';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';
import { getTypeArguments } from './getTypeArguments';
import { getTypeFlags, isTypeFlagSet } from './typeFlagUtils';
const log = debug('typescript-eslint:eslint-plugin:utils:types');
/**
* Checks if the given type is (or accepts) nullable
* @param isReceiver true if the type is a receiving type (i.e. the type of a called function's parameter)
*/
export function isNullableType(
type: ts.Type,
{
isReceiver = false,
allowUndefined = true,
}: { isReceiver?: boolean; allowUndefined?: boolean } = {},
): boolean {
const flags = getTypeFlags(type);
if (isReceiver && flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
}
if (allowUndefined) {
return (flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
}
return (flags & ts.TypeFlags.Null) !== 0;
}
/**
* Checks if the given type is either an array type,
* or a union made up solely of array types.
*/
export function isTypeArrayTypeOrUnionOfArrayTypes(
type: ts.Type,
checker: ts.TypeChecker,
): boolean {
for (const t of tsutils.unionTypeParts(type)) {
if (!checker.isArrayType(t)) {
return false;
}
}
return true;
}
/**
* @returns true if the type is `never`
*/
export function isTypeNeverType(type: ts.Type): boolean {
return isTypeFlagSet(type, ts.TypeFlags.Never);
}
/**
* @returns true if the type is `unknown`
*/
export function isTypeUnknownType(type: ts.Type): boolean {
return isTypeFlagSet(type, ts.TypeFlags.Unknown);
}
// https://github.com/microsoft/TypeScript/blob/42aa18bf442c4df147e30deaf27261a41cbdc617/src/compiler/types.ts#L5157
const Nullable = ts.TypeFlags.Undefined | ts.TypeFlags.Null;
// https://github.com/microsoft/TypeScript/blob/42aa18bf442c4df147e30deaf27261a41cbdc617/src/compiler/types.ts#L5187
const ObjectFlagsType =
ts.TypeFlags.Any |
Nullable |
ts.TypeFlags.Never |
ts.TypeFlags.Object |
ts.TypeFlags.Union |
ts.TypeFlags.Intersection;
export function isTypeReferenceType(type: ts.Type): type is ts.TypeReference {
if ((type.flags & ObjectFlagsType) === 0) {
return false;
}
const objectTypeFlags = (type as ts.ObjectType).objectFlags;
return (objectTypeFlags & ts.ObjectFlags.Reference) !== 0;
}
/**
* @returns true if the type is `any`
*/
export function isTypeAnyType(type: ts.Type): boolean {
if (isTypeFlagSet(type, ts.TypeFlags.Any)) {
if (type.intrinsicName === 'error') {
log('Found an "error" any type');
}
return true;
}
return false;
}
/**
* @returns true if the type is `any[]`
*/
export function isTypeAnyArrayType(
type: ts.Type,
checker: ts.TypeChecker,
): boolean {
return (
checker.isArrayType(type) &&
isTypeAnyType(
// getTypeArguments was only added in TS3.7
getTypeArguments(type, checker)[0],
)
);
}
/**
* @returns true if the type is `unknown[]`
*/
export function isTypeUnknownArrayType(
type: ts.Type,
checker: ts.TypeChecker,
): boolean {
return (
checker.isArrayType(type) &&
isTypeUnknownType(
// getTypeArguments was only added in TS3.7
getTypeArguments(type, checker)[0],
)
);
}
export enum AnyType {
Any,
AnyArray,
Safe,
}
/**
* @returns `AnyType.Any` if the type is `any`, `AnyType.AnyArray` if the type is `any[]` or `readonly any[]`,
* otherwise it returns `AnyType.Safe`.
*/
export function isAnyOrAnyArrayTypeDiscriminated(
node: ts.Node,
checker: ts.TypeChecker,
): AnyType {
const type = checker.getTypeAtLocation(node);
if (isTypeAnyType(type)) {
return AnyType.Any;
}
if (isTypeAnyArrayType(type, checker)) {
return AnyType.AnyArray;
}
return AnyType.Safe;
}
/**
* @returns Whether a type is an instance of the parent type, including for the parent's base types.
*/
export function typeIsOrHasBaseType(
type: ts.Type,
parentType: ts.Type,
): boolean {
const parentSymbol = parentType.getSymbol();
if (!type.getSymbol() || !parentSymbol) {
return false;
}
const typeAndBaseTypes = [type];
const ancestorTypes = type.getBaseTypes();
if (ancestorTypes) {
typeAndBaseTypes.push(...ancestorTypes);
}
for (const baseType of typeAndBaseTypes) {
const baseSymbol = baseType.getSymbol();
if (baseSymbol && baseSymbol.name === parentSymbol.name) {
return true;
}
}
return false;
}
export function isTypeBigIntLiteralType(
type: ts.Type,
): type is ts.BigIntLiteralType {
return isTypeFlagSet(type, ts.TypeFlags.BigIntLiteral);
}
export function isTypeTemplateLiteralType(
type: ts.Type,
): type is ts.TemplateLiteralType {
return isTypeFlagSet(type, ts.TypeFlags.TemplateLiteral);
}