0
# Type Checking and Validation
1
2
Comprehensive type guards and validation utilities for runtime type checking, safe property access, and data validation across Vue's codebase.
3
4
## Capabilities
5
6
### Basic Type Guards
7
8
Core type checking functions that provide TypeScript type narrowing and runtime validation.
9
10
```typescript { .api }
11
/**
12
* Check if value is a string
13
*/
14
function isString(val: unknown): val is string;
15
16
/**
17
* Check if value is a function
18
*/
19
function isFunction(val: unknown): val is Function;
20
21
/**
22
* Check if value is a symbol
23
*/
24
function isSymbol(val: unknown): val is symbol;
25
26
/**
27
* Check if value is an object (not null)
28
*/
29
function isObject(val: unknown): val is Record<any, any>;
30
31
/**
32
* Check if value is a plain object
33
*/
34
function isPlainObject(val: unknown): val is object;
35
```
36
37
### Collection Type Guards
38
39
Type guards for JavaScript collection types and arrays.
40
41
```typescript { .api }
42
/**
43
* Alias for Array.isArray with proper typing
44
*/
45
const isArray: typeof Array.isArray;
46
47
/**
48
* Check if value is a Map
49
*/
50
function isMap(val: unknown): val is Map<any, any>;
51
52
/**
53
* Check if value is a Set
54
*/
55
function isSet(val: unknown): val is Set<any>;
56
```
57
58
### Built-in Object Type Guards
59
60
Type guards for built-in JavaScript objects and primitives.
61
62
```typescript { .api }
63
/**
64
* Check if value is a Date object
65
*/
66
function isDate(val: unknown): val is Date;
67
68
/**
69
* Check if value is a RegExp object
70
*/
71
function isRegExp(val: unknown): val is RegExp;
72
73
/**
74
* Check if value is a Promise
75
*/
76
function isPromise<T = any>(val: unknown): val is Promise<T>;
77
```
78
79
### Property and Key Validation
80
81
Utilities for validating object properties and keys.
82
83
```typescript { .api }
84
/**
85
* Check if object has own property (type-safe)
86
* @param val - Object to check
87
* @param key - Property key to check for
88
* @returns Type predicate indicating key exists
89
*/
90
function hasOwn(val: object, key: string | symbol): key is keyof typeof val;
91
92
/**
93
* Check if key represents an integer-like string
94
* @param key - Key to validate
95
* @returns True if key is integer-like ("0", "123", etc.)
96
*/
97
function isIntegerKey(key: unknown): boolean;
98
```
99
100
### Vue-Specific Type Checks
101
102
Type validation utilities specific to Vue's component and template system.
103
104
```typescript { .api }
105
/**
106
* Check if string is an event handler name (starts with "on" + uppercase)
107
* @param key - Property name to check
108
* @returns True if key follows event handler pattern
109
*/
110
function isOn(key: string): boolean;
111
112
/**
113
* Check if string is a model listener (v-model update event)
114
* @param key - Property name to check
115
* @returns Type predicate for update event names
116
*/
117
function isModelListener(key: string): key is `onUpdate:${string}`;
118
119
/**
120
* Check if property name is reserved by Vue
121
* @param key - Property name to check
122
* @returns True if property is reserved (key, ref, etc.)
123
*/
124
function isReservedProp(key: string): boolean;
125
126
/**
127
* Check if directive name is a built-in Vue directive
128
* @param key - Directive name to check
129
* @returns True if directive is built-in (v-if, v-for, etc.)
130
*/
131
function isBuiltInDirective(key: string): boolean;
132
```
133
134
### Type String Utilities
135
136
Low-level utilities for working with JavaScript's type system and toString representations.
137
138
```typescript { .api }
139
/**
140
* Reference to Object.prototype.toString for type checking
141
*/
142
const objectToString: typeof Object.prototype.toString;
143
144
/**
145
* Get the type string representation of a value
146
* @param value - Value to get type string for
147
* @returns Type string like "[object String]"
148
*/
149
function toTypeString(value: unknown): string;
150
151
/**
152
* Extract the raw type name from a type string
153
* @param value - Value to get raw type for
154
* @returns Raw type name like "String", "Array", etc.
155
*/
156
function toRawType(value: unknown): string;
157
```
158
159
**Usage Examples:**
160
161
```typescript
162
import { isString, isObject, hasOwn, isOn, toRawType } from "@vue/shared";
163
164
// Basic type checking with type narrowing
165
function processValue(value: unknown) {
166
if (isString(value)) {
167
// TypeScript knows value is string here
168
return value.toUpperCase();
169
}
170
171
if (isObject(value)) {
172
// TypeScript knows value is Record<any, any> here
173
return Object.keys(value);
174
}
175
}
176
177
// Safe property access
178
function getProperty(obj: object, key: string) {
179
if (hasOwn(obj, key)) {
180
// TypeScript knows key exists on obj
181
return obj[key];
182
}
183
return undefined;
184
}
185
186
// Vue-specific checks
187
function isEventProp(key: string) {
188
return isOn(key); // true for "onClick", "onMouseover", etc.
189
}
190
191
// Type introspection
192
const type = toRawType(new Date()); // "Date"
193
const type2 = toRawType([]); // "Array"
194
```