You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/typescript.md
+25-41
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
---
2
-
title: 'TypeScript'
2
+
title: 'TypeScript Guide'
3
3
---
4
4
5
-
# TypeScript
5
+
# TypeScript Integration
6
6
7
-
In order to provide flexibility with TypeScript, Vue 3 Select Component has been written in TypeScript. This means that you can take advantage of TypeScript's type checking and autocompletion features.
7
+
Vue 3 Select Component is built with TypeScript to provide complete type safety and excellent IDE support through autocompletion and type checking.
8
8
9
-
## Generics with Vue & TypeScript
9
+
## Understanding generic types
10
10
11
-
Vue 3 Select Component uses a feature that has been released on Vue 3.3 called [**Generics**](https://vuejs.org/api/sfc-script-setup.html#generics).
11
+
This component leverages Vue 3.3's [Generics](https://vuejs.org/api/sfc-script-setup.html#generics) feature for enhanced type flexibility.
12
12
13
-
Generics allow you to define a type that can be used in multiple places with different types. This is useful when you want to create a component that can be used with different types of data.
13
+
Generics enable type reusability across different data types, making the component highly adaptable to various use cases.
14
14
15
-
A common type taking use of the Vue Generic is the `Option` type, which is used to define the `:options` prop of the select component:
15
+
The core `Option` type demonstrates this through its generic implementation:
16
16
17
17
```ts
18
18
typeOption<T> = {
@@ -22,25 +22,21 @@ type Option<T> = {
22
22
};
23
23
```
24
24
25
-
## Customizing `option.value` type
25
+
## Configuring value types
26
26
27
27
::: info
28
-
Ensure you are familiar with the [`:options` prop](/props#options)before reading this section.
28
+
Please review the [`:options` prop](/props#options)documentation first.
29
29
:::
30
30
31
-
By default, the `value` property of the option object is a `string`. However, it is possible to use a different type, such as a `number`.
32
-
33
-
To do this, import the `Option` type from the component and define a custom type that extends the `Option` type with a generic type:
31
+
While the default type for `option.value` is `string`, you can specify different types like `number`. Import and extend the `Option` type with your preferred generic type:
34
32
35
33
```vue
36
34
<script setup lang="ts">
37
35
import type { Option } from "vue3-select-component";
38
36
import { ref } from "vue";
39
37
import VueSelect from "vue3-select-component";
40
38
41
-
// Define a custom type for the option value.
42
-
// It takes a generic type that defines the type of the `value` property.
43
-
// In this case, the `value` property is a `number`.
`getOptionValue` and `getOptionLabel` props are not compatible with the type-safety of the component. Therefore, you should use them with caution and only as a last resort.
134
+
Using `getOptionValue` and `getOptionLabel` props bypasses component type-safety. Use these as a last resort.
149
135
:::
150
136
151
-
If you're using the `getOptionValue` or `getOptionLabel` props, there are a few gotchas to be aware of with the types:
137
+
When using custom label/value functions, keep in mind:
152
138
153
-
-Local array of options cannot be typed as `Option<T>[]`
154
-
-When passing the array of options to the component, you need to cast it to `unknown` then `Option<T>[]`.
139
+
-Don't type local options array as `Option<T>[]`
140
+
-Cast options to `Option<T>[]` at the component level
155
141
156
-
Here's an example usage of the `getOptionValue` and `getOptionLabel` props with TypeScript:
142
+
Example implementation:
157
143
158
144
```vue
159
145
<script setup lang="ts">
160
146
import type { Option } from "vue3-select-component";
161
147
162
148
const activeRole = ref<string>("");
163
149
164
-
// You cannot type the `roleOptions` as `Option<string>[]`.
165
150
const roleOptions = [
166
151
{ id: "Admin", key: "admin" },
167
152
{ id: "User", key: "user" },
@@ -170,7 +155,6 @@ const roleOptions = [
170
155
</script>
171
156
172
157
<template>
173
-
<!-- Casting of the `roleOptions` must be done at the `:options` prop-level. -->
174
158
<VueSelect
175
159
v-model="activeRole"
176
160
:options="(roleOptions as unknown as Option<string>[])"
0 commit comments