|
| 1 | +# TypeScript Usage |
| 2 | + |
| 3 | +VueTypes is written in TypeScript and comes with full builtin types support. |
| 4 | + |
| 5 | +Most of the validators can infer the prop type from their configuration: |
| 6 | + |
| 7 | +```ts |
| 8 | +props: { |
| 9 | + // prop type is `string` |
| 10 | + name: string(), |
| 11 | + // ERROR: Argument of type 'boolean' is not assignable to parameter of type 'string' |
| 12 | + surname: string().def(false), |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +## Optional type constraint |
| 17 | + |
| 18 | +Some validators accepts an optional type argument to refine their TS typing. |
| 19 | + |
| 20 | +### any<T = any> |
| 21 | + |
| 22 | +```ts |
| 23 | +props: { |
| 24 | + unknownProp: any<unknown>() |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### string<T = string> |
| 29 | + |
| 30 | +Accepts both strings and enums. |
| 31 | + |
| 32 | +```ts |
| 33 | +props: { |
| 34 | + // use a union type to constrain the string type |
| 35 | + color: string<'red' | 'green'>() |
| 36 | +} |
| 37 | + |
| 38 | +enum Colors { |
| 39 | + red = 'red' |
| 40 | + green = 'green' |
| 41 | +} |
| 42 | + |
| 43 | +props: { |
| 44 | + // same as above, but with an enum |
| 45 | + color: string<Colors>() |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +::: tip |
| 50 | +The same prop type can be expressed using `oneOf` which will also perform a validation at runtime: |
| 51 | + |
| 52 | +```ts |
| 53 | +props: { |
| 54 | + genre: oneOf(['red', 'green'] as const) |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +::: |
| 59 | + |
| 60 | +### number<T = number> and integer<T = number> |
| 61 | + |
| 62 | +```ts |
| 63 | +props: { |
| 64 | + // use a union type to constrain the number type |
| 65 | + count: number<1 | 2>() |
| 66 | + |
| 67 | + countInt: integer<1 | 2>() |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### func<T = (...args: any[]) => any> |
| 72 | + |
| 73 | +Useful to type event handlers and function return types. |
| 74 | + |
| 75 | +```ts |
| 76 | +type EventFn = (e: Event) => void |
| 77 | +type AsyncFn = () => Promise<string[]> |
| 78 | + |
| 79 | +props: { |
| 80 | + onClick: fn<EventFn>() |
| 81 | + loadStrings: fn<AsyncFn>() |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### object<T = { [key: string]: any }> |
| 86 | + |
| 87 | +```ts |
| 88 | +interface User { |
| 89 | + name: string |
| 90 | + // ... |
| 91 | +} |
| 92 | + |
| 93 | +props: { |
| 94 | + user: object<User>() |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +::: tip |
| 99 | +To have both compile-time and runtime validation, you can use `shape`: |
| 100 | + |
| 101 | +```ts |
| 102 | +interface User { |
| 103 | + name: string |
| 104 | + // ... |
| 105 | +} |
| 106 | + |
| 107 | +props: { |
| 108 | + user: shape<User>({ |
| 109 | + name: string().isRequired, |
| 110 | + }) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +::: |
| 115 | + |
| 116 | +### array<T = unknown> |
| 117 | + |
| 118 | +The validator accepts an argument defining the contained items type. |
| 119 | + |
| 120 | +```ts |
| 121 | +interface User { |
| 122 | + name: string |
| 123 | + // ... |
| 124 | +} |
| 125 | + |
| 126 | +props: { |
| 127 | + // array of numbers |
| 128 | + sizes: array<number>() |
| 129 | + // array of users |
| 130 | + users: array<User>() |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +::: tip |
| 135 | +The same prop types can be expressed using `arrayOf` which will also perform a validation at runtime: |
| 136 | + |
| 137 | +```ts |
| 138 | +interface User { |
| 139 | + name: string |
| 140 | + // ... |
| 141 | +} |
| 142 | + |
| 143 | +props: { |
| 144 | + sizes: arrayOf(Number) |
| 145 | + // set the object() validator type to User |
| 146 | + users: arrayOf(object<User>()) |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +::: |
| 151 | + |
| 152 | +### OneOfType\<T> |
| 153 | + |
| 154 | +You can use a union type to specify the expected types. |
| 155 | + |
| 156 | +```ts |
| 157 | +interface User { |
| 158 | + name: string |
| 159 | + // ... |
| 160 | +} |
| 161 | + |
| 162 | +props: { |
| 163 | + // string or instance of User |
| 164 | + theProp: oneOfType<string | User>([String, Object]) |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +::: tip |
| 169 | +The same prop types can be expressed composing VueTypes validators: |
| 170 | + |
| 171 | +```ts |
| 172 | +interface User { |
| 173 | + name: string |
| 174 | + // ... |
| 175 | +} |
| 176 | + |
| 177 | +props: { |
| 178 | + // same as above |
| 179 | + theProp: oneOfType([String, object<User>()]) |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +::: |
| 184 | + |
| 185 | +### shape\<T> |
| 186 | + |
| 187 | +Setting the type argument provides type checking on top of runtime validation: |
| 188 | + |
| 189 | +```ts |
| 190 | +interface User { |
| 191 | + name: string |
| 192 | + id?: string |
| 193 | +} |
| 194 | + |
| 195 | +props: { |
| 196 | + user: shape<User>({ |
| 197 | + name: string().isRequired, |
| 198 | + id: string(), |
| 199 | + }) |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +### custom\<T> |
| 204 | + |
| 205 | +You can define the type of the value received by the validator function. |
| 206 | + |
| 207 | +```ts |
| 208 | +props: { |
| 209 | + // function argument is of type string |
| 210 | + nonEmptyString: custom<string>((v) => typeof v === 'string' && v.length > 0) |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +::: tip |
| 215 | +This validator can be used for tuple props: |
| 216 | + |
| 217 | +```ts |
| 218 | +type Pair = [string, number] |
| 219 | + |
| 220 | +props: { |
| 221 | + tuple: custom<Pair>( |
| 222 | + ([a, b]) => typeof a === 'string' && typeof b === 'number', |
| 223 | + ) |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +::: |
| 228 | + |
| 229 | +### oneOf |
| 230 | + |
| 231 | +This validator does not support type arguments, but you can use [const assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions) on the expected values to constrain the validators type: |
| 232 | + |
| 233 | +```ts |
| 234 | +props: { |
| 235 | + // ERROR: Argument of type '"small"' is not assignable |
| 236 | + // to parameter of type '"large" | "medium"'. |
| 237 | + sizes: oneOf(['large', 'medium'] as const).def('small') |
| 238 | +} |
| 239 | +``` |
0 commit comments