/
predicates.ts
152 lines (136 loc) · 3.46 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
import debug from 'debug';
import { unionTypeParts } from 'tsutils';
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;
} else {
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 unionTypeParts(type)) {
if (!checker.isArrayType(t)) {
return false;
}
}
return true;
}
/**
* @returns true if the type is `unknown`
*/
export function isTypeUnknownType(type: ts.Type): boolean {
return isTypeFlagSet(type, ts.TypeFlags.Unknown);
}
/**
* @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;
}