/
isTypeReadonly.ts
198 lines (168 loc) · 5.53 KB
/
isTypeReadonly.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
// @ts-nocheck
import tsutils from "~/conditional-imports/tsutils";
import ts from "~/conditional-imports/typescript";
import { nullThrows, NullThrowsReasons } from "./nullThrows";
import { getTypeOfPropertyOfType } from "./propertyTypes";
const enum Readonlyness {
/** the type cannot be handled by the function */
UnknownType = 1,
/** the type is mutable */
Mutable = 2,
/** the type is readonly */
Readonly = 3,
}
function isTypeReadonlyArrayOrTuple(
checker: ts.TypeChecker,
type: ts.Type,
seenTypes: Set<ts.Type>
): Readonlyness {
function checkTypeArguments(arrayType: ts.TypeReference): Readonlyness {
const typeArguments = checker.getTypeArguments
? checker.getTypeArguments(arrayType)
: arrayType.typeArguments ?? [];
// this shouldn't happen in reality as:
// - tuples require at least 1 type argument
// - ReadonlyArray requires at least 1 type argument
/* istanbul ignore if */ if (typeArguments.length === 0) {
return Readonlyness.Readonly;
}
// validate the element types are also readonly
if (
typeArguments.some(
(typeArg) =>
isTypeReadonlyRecurser(checker, typeArg, seenTypes) ===
Readonlyness.Mutable
)
) {
return Readonlyness.Mutable;
}
return Readonlyness.Readonly;
}
if (checker.isArrayType(type)) {
const symbol = nullThrows(
type.getSymbol(),
NullThrowsReasons.MissingToken("symbol", "array type")
);
const escapedName = symbol.getEscapedName();
if (escapedName === "Array") {
return Readonlyness.Mutable;
}
return checkTypeArguments(type);
}
if (checker.isTupleType(type)) {
if (!type.target.readonly) {
return Readonlyness.Mutable;
}
return checkTypeArguments(type);
}
return Readonlyness.UnknownType;
}
function isTypeReadonlyObject(
checker: ts.TypeChecker,
type: ts.Type,
seenTypes: Set<ts.Type>
): Readonlyness {
function checkIndexSignature(kind: ts.IndexKind): Readonlyness {
const indexInfo = checker.getIndexInfoOfType(type, kind);
if (indexInfo) {
return indexInfo.isReadonly
? Readonlyness.Readonly
: Readonlyness.Mutable;
}
return Readonlyness.UnknownType;
}
const properties = type.getProperties();
if (properties.length) {
// ensure the properties are marked as readonly
for (const property of properties) {
if (!tsutils.isPropertyReadonlyInType(type, property.getEscapedName(), checker)) {
return Readonlyness.Mutable;
}
}
// all properties were readonly
// now ensure that all of the values are readonly also.
// do this after checking property readonly-ness as a perf optimization,
// as we might be able to bail out early due to a mutable property before
// doing this deep, potentially expensive check.
for (const property of properties) {
const propertyType = nullThrows(
getTypeOfPropertyOfType(checker, type, property),
NullThrowsReasons.MissingToken(`property "${property.name}"`, "type")
);
// handle recursive types.
// we only need this simple check, because a mutable recursive type will break via the above prop readonly check
if (seenTypes.has(propertyType)) {
continue;
}
if (
isTypeReadonlyRecurser(checker, propertyType, seenTypes) ===
Readonlyness.Mutable
) {
return Readonlyness.Mutable;
}
}
}
const isStringIndexSigReadonly = checkIndexSignature(ts.IndexKind.String);
if (isStringIndexSigReadonly === Readonlyness.Mutable) {
return isStringIndexSigReadonly;
}
const isNumberIndexSigReadonly = checkIndexSignature(ts.IndexKind.Number);
if (isNumberIndexSigReadonly === Readonlyness.Mutable) {
return isNumberIndexSigReadonly;
}
return Readonlyness.Readonly;
}
// a helper function to ensure the seenTypes map is always passed down, except by the external caller
function isTypeReadonlyRecurser(
checker: ts.TypeChecker,
type: ts.Type,
seenTypes: Set<ts.Type>
): Readonlyness.Readonly | Readonlyness.Mutable {
seenTypes.add(type);
if (tsutils.isUnionType(type)) {
// all types in the union must be readonly
const result = tsutils.unionTypeParts(type).every((t) =>
isTypeReadonlyRecurser(checker, t, seenTypes)
);
const readonlyness = result ? Readonlyness.Readonly : Readonlyness.Mutable;
return readonlyness;
}
// all non-object, non-intersection types are readonly.
// this should only be primitive types
if (!tsutils.isObjectType(type) && !tsutils.isUnionOrIntersectionType(type)) {
return Readonlyness.Readonly;
}
// pure function types are readonly
if (
type.getCallSignatures().length > 0 &&
type.getProperties().length === 0
) {
return Readonlyness.Readonly;
}
const isReadonlyArray = isTypeReadonlyArrayOrTuple(checker, type, seenTypes);
if (isReadonlyArray !== Readonlyness.UnknownType) {
return isReadonlyArray;
}
const isReadonlyObject = isTypeReadonlyObject(checker, type, seenTypes);
/* istanbul ignore else */ if (
isReadonlyObject !== Readonlyness.UnknownType
) {
return isReadonlyObject;
}
throw new Error("Unhandled type");
}
/**
* Checks if the given type is readonly
*/
function isTypeReadonly(checker: ts.TypeChecker, type: ts.Type): boolean {
if (ts === undefined) {
throw new Error("TypeScript not found.");
}
if (tsutils === undefined) {
throw new Error("tsutils not found.");
}
return (
isTypeReadonlyRecurser(checker, type, new Set()) === Readonlyness.Readonly
);
}
export { isTypeReadonly };