-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathTypescriptGuards.ts
208 lines (191 loc) · 5.48 KB
/
TypescriptGuards.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
import {
ArrayBindingPattern,
ConstructorDeclaration,
ExportDeclaration,
ExternalModuleReference,
FunctionDeclaration,
Identifier,
ImportDeclaration,
ImportEqualsDeclaration,
MethodDeclaration,
MethodSignature,
NamedExports,
NamedImports,
NamespaceImport,
Node,
ObjectBindingPattern,
PropertyDeclaration,
PropertySignature,
StringLiteral,
SyntaxKind,
} from 'typescript';
/**
* Determines if the given node is an ImportDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is ImportDeclaration}
*/
export function isImportDeclaration(node?: Node): node is ImportDeclaration {
return node !== undefined && node.kind === SyntaxKind.ImportDeclaration;
}
/**
* Determines if the given node is an ImportEqualsDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is ImportEqualsDeclaration}
*/
export function isImportEqualsDeclaration(node?: Node): node is ImportEqualsDeclaration {
return node !== undefined && node.kind === SyntaxKind.ImportEqualsDeclaration;
}
/**
* Determines if the given node is a NamespaceImport.
*
* @export
* @param {Node} [node]
* @returns {node is NamespaceImport}
*/
export function isNamespaceImport(node?: Node): node is NamespaceImport {
return node !== undefined && node.kind === SyntaxKind.NamespaceImport;
}
/**
* Determines if the given node are NamedImports.
*
* @export
* @param {Node} [node]
* @returns {node is NamedImports}
*/
export function isNamedImports(node?: Node): node is NamedImports {
return node !== undefined && node.kind === SyntaxKind.NamedImports;
}
/**
* Determines if the given node are NamedExports.
*
* @export
* @param {Node} [node]
* @returns {node is NamedExports}
*/
export function isNamedExports(node?: Node): node is NamedExports {
return node !== undefined && node.kind === SyntaxKind.NamedExports;
}
/**
* Determines if the given node is a StringLiteral.
*
* @export
* @param {Node} [node]
* @returns {node is StringLiteral}
*/
export function isStringLiteral(node?: Node): node is StringLiteral {
return node !== undefined && node.kind === SyntaxKind.StringLiteral;
}
/**
* Determines if the given node is an Identifier.
*
* @export
* @param {Node} [node]
* @returns {node is Identifier}
*/
export function isIdentifier(node?: Node): node is Identifier {
return node !== undefined && node.kind === SyntaxKind.Identifier;
}
/**
* Determines if the given node is an ExternalModuleReference.
*
* @export
* @param {Node} [node]
* @returns {node is ExternalModuleReference}
*/
export function isExternalModuleReference(node?: Node): node is ExternalModuleReference {
return node !== undefined && node.kind === SyntaxKind.ExternalModuleReference;
}
/**
* Determines if the given node is an ExportDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is ExportDeclaration}
*/
export function isExportDeclaration(node?: Node): node is ExportDeclaration {
return node !== undefined && node.kind === SyntaxKind.ExportDeclaration;
}
/**
* Determines if the given node is an ObjectBindingPattern (i.e. let {x, y} = foo).
*
* @export
* @param {Node} [node]
* @returns {node is ObjectBindingPattern}
*/
export function isObjectBindingPattern(node?: Node): node is ObjectBindingPattern {
return node !== undefined && node.kind === SyntaxKind.ObjectBindingPattern;
}
/**
* Determines if the given node is an ArrayBindingPattern (i.e. let [x, y] = foo).
*
* @export
* @param {Node} [node]
* @returns {node is ArrayBindingPattern}
*/
export function isArrayBindingPattern(node?: Node): node is ArrayBindingPattern {
return node !== undefined && node.kind === SyntaxKind.ArrayBindingPattern;
}
/**
* Determines if the given node is a FunctionDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is FunctionDeclaration}
*/
export function isFunctionDeclaration(node?: Node): node is FunctionDeclaration {
return node !== undefined && node.kind === SyntaxKind.FunctionDeclaration;
}
/**
* Determines if the given node is a MethodSignature.
*
* @export
* @param {Node} [node]
* @returns {node is MethodSignature}
*/
export function isMethodSignature(node?: Node): node is MethodSignature {
return node !== undefined && node.kind === SyntaxKind.MethodSignature;
}
/**
* Determines if the given node is a PropertySignature.
*
* @export
* @param {Node} [node]
* @returns {node is PropertySignature}
*/
export function isPropertySignature(node?: Node): node is PropertySignature {
return node !== undefined && node.kind === SyntaxKind.PropertySignature;
}
/**
* Determines if the given node is a MethodDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is MethodDeclaration}
*/
export function isMethodDeclaration(node?: Node): node is MethodDeclaration {
return node !== undefined && node.kind === SyntaxKind.MethodDeclaration;
}
/**
* Determines if the given node is a PropertyDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is PropertyDeclaration}
*/
export function isPropertyDeclaration(node?: Node): node is PropertyDeclaration {
return node !== undefined && node.kind === SyntaxKind.PropertyDeclaration;
}
/**
* Determines if the given node is a ConstructorDeclaration.
*
* @export
* @param {Node} [node]
* @returns {node is ConstructorDeclaration}
*/
export function isConstructorDeclaration(node?: Node): node is ConstructorDeclaration {
return node !== undefined && node.kind === SyntaxKind.Constructor;
}