0
# Type System Guards
1
2
Type guard functions for TypeScript's type system using `ts.TypeFlags`. These functions provide type-safe narrowing of `ts.Type` union types to specific type system types, enabling safe access to type-specific properties and methods in the TypeScript compiler's type system.
3
4
## Capabilities
5
6
### Union and Intersection Types
7
8
Type guards for composite types that combine multiple types.
9
10
```typescript { .api }
11
/**
12
* Check if type is a union type (A | B)
13
* @param type - Type to check
14
* @returns true if type is ts.UnionType
15
*/
16
function isUnionType(type: ts.Type): type is ts.UnionType;
17
18
/**
19
* Check if type is an intersection type (A & B)
20
* @param type - Type to check
21
* @returns true if type is ts.IntersectionType
22
*/
23
function isIntersectionType(type: ts.Type): type is ts.IntersectionType;
24
25
/**
26
* Check if type is either union or intersection type
27
* @param type - Type to check
28
* @returns true if type is ts.UnionOrIntersectionType
29
*/
30
function isUnionOrIntersectionType(type: ts.Type): type is ts.UnionOrIntersectionType;
31
```
32
33
### Object and Interface Types
34
35
Type guards for object-based types including interfaces and object literals.
36
37
```typescript { .api }
38
/**
39
* Check if type is an object type
40
* @param type - Type to check
41
* @returns true if type is ts.ObjectType
42
*/
43
function isObjectType(type: ts.Type): type is ts.ObjectType;
44
45
/**
46
* Check if type is an interface type
47
* @param type - Type to check
48
* @returns true if type is ts.InterfaceType
49
*/
50
function isInterfaceType(type: ts.Type): type is ts.InterfaceType;
51
```
52
53
### Reference and Generic Types
54
55
Type guards for type references and generic type constructs.
56
57
```typescript { .api }
58
/**
59
* Check if type is a type reference (reference to named type)
60
* @param type - Type to check
61
* @returns true if type is ts.TypeReference
62
*/
63
function isTypeReference(type: ts.Type): type is ts.TypeReference;
64
65
/**
66
* Check if type is a generic type
67
* @param type - Type to check
68
* @returns true if type is ts.GenericType
69
*/
70
function isGenericType(type: ts.Type): type is ts.GenericType;
71
```
72
73
### Type Parameters and Variables
74
75
Type guards for type parameters and type variables.
76
77
```typescript { .api }
78
/**
79
* Check if type is a type parameter (T in generic)
80
* @param type - Type to check
81
* @returns true if type is ts.TypeParameter
82
*/
83
function isTypeParameter(type: ts.Type): type is ts.TypeParameter;
84
85
/**
86
* Check if type is a type variable (type parameter or indexed access)
87
* @param type - Type to check
88
* @returns true if type is ts.TypeParameter or ts.IndexedAccessType
89
*/
90
function isTypeVariable(type: ts.Type): type is ts.TypeParameter | ts.IndexedAccessType;
91
```
92
93
### Literal and Primitive Types
94
95
Type guards for literal values and primitive type checking.
96
97
```typescript { .api }
98
/**
99
* Check if type is a literal type (string/number/boolean literal)
100
* @param type - Type to check
101
* @returns true if type is ts.LiteralType
102
*/
103
function isLiteralType(type: ts.Type): type is ts.LiteralType;
104
105
/**
106
* Check if type is a unique ES symbol type
107
* @param type - Type to check
108
* @returns true if type is ts.UniqueESSymbolType
109
*/
110
function isUniqueESSymbolType(type: ts.Type): type is ts.UniqueESSymbolType;
111
```
112
113
### Advanced Type Constructs
114
115
Type guards for advanced TypeScript type system features.
116
117
```typescript { .api }
118
/**
119
* Check if type is an indexed access type (T[K])
120
* @param type - Type to check
121
* @returns true if type is ts.IndexedAccessType
122
*/
123
function isIndexedAccessType(type: ts.Type): type is ts.IndexedAccessType;
124
125
/**
126
* Check if type is a conditional type (T extends U ? X : Y)
127
* @param type - Type to check
128
* @returns true if type is ts.ConditionalType
129
*/
130
function isConditionalType(type: ts.Type): type is ts.ConditionalType;
131
132
/**
133
* Check if type is a substitution type
134
* @param type - Type to check
135
* @returns true if type is ts.SubstitutionType
136
*/
137
function isSubstitutionType(type: ts.Type): type is ts.SubstitutionType;
138
139
/**
140
* Check if type is an instantiable type
141
* @param type - Type to check
142
* @returns true if type is ts.InstantiableType
143
*/
144
function isInstantiableType(type: ts.Type): type is ts.InstantiableType;
145
```
146
147
### Enum Types
148
149
Type guards for enum type checking.
150
151
```typescript { .api }
152
/**
153
* Check if type is an enum type
154
* @param type - Type to check
155
* @returns true if type is ts.EnumType
156
*/
157
function isEnumType(type: ts.Type): type is ts.EnumType;
158
```
159
160
### Tuple Types
161
162
Type guards for tuple type checking (TypeScript 3.0+).
163
164
```typescript { .api }
165
/**
166
* Check if type is a tuple type (TypeScript 3.0+)
167
* @param type - Type to check
168
* @returns true if type is ts.TupleType
169
*/
170
function isTupleType(type: ts.Type): type is ts.TupleType;
171
172
/**
173
* Check if type is a tuple type reference (TypeScript 3.0+)
174
* @param type - Type to check
175
* @returns true if type is ts.TypeReference with ts.TupleType target
176
*/
177
function isTupleTypeReference(type: ts.Type): type is ts.TypeReference & {target: ts.TupleType};
178
```
179
180
**Usage Examples:**
181
182
```typescript
183
import * as ts from "typescript";
184
import { isUnionType, isLiteralType, isObjectType } from "tsutils/typeguard";
185
186
function analyzeType(checker: ts.TypeChecker, type: ts.Type) {
187
if (isUnionType(type)) {
188
// type is now typed as ts.UnionType
189
console.log("Union type with", type.types.length, "members");
190
type.types.forEach((memberType, index) => {
191
console.log(`Member ${index}:`, checker.typeToString(memberType));
192
});
193
}
194
195
if (isLiteralType(type)) {
196
// type is now typed as ts.LiteralType
197
console.log("Literal type value:", type.value);
198
}
199
200
if (isObjectType(type)) {
201
// type is now typed as ts.ObjectType
202
console.log("Object type flags:", type.objectFlags);
203
console.log("Properties:", type.symbol?.members?.size || 0);
204
}
205
}
206
207
// Example with type checking
208
function checkTypeProperties(checker: ts.TypeChecker, node: ts.Expression) {
209
const type = checker.getTypeAtLocation(node);
210
211
if (isUnionType(type)) {
212
// Analyze each union member
213
const stringTypes = type.types.filter(t =>
214
t.flags & ts.TypeFlags.String || isLiteralType(t) && typeof t.value === 'string'
215
);
216
217
if (stringTypes.length > 0) {
218
console.log("Union contains string types");
219
}
220
}
221
}
222
```