Skip to content

Commit fbec5b3

Browse files
committed
feat(docs): update /typescript docs
1 parent 20d46c2 commit fbec5b3

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

Diff for: docs/.vitepress/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default defineConfig({
3838
{ text: "Slots", link: "/slots" },
3939
{ text: "Events", link: "/events" },
4040
{ text: "Styling", link: "/styling" },
41-
{ text: "TypeScript", link: "/typescript" },
41+
{ text: "TypeScript Guide", link: "/typescript" },
4242
],
4343
},
4444
{

Diff for: docs/typescript.md

+25-41
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
2-
title: 'TypeScript'
2+
title: 'TypeScript Guide'
33
---
44

5-
# TypeScript
5+
# TypeScript Integration
66

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.
88

9-
## Generics with Vue & TypeScript
9+
## Understanding generic types
1010

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.
1212

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.
1414

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:
1616

1717
```ts
1818
type Option<T> = {
@@ -22,25 +22,21 @@ type Option<T> = {
2222
};
2323
```
2424

25-
## Customizing `option.value` type
25+
## Configuring value types
2626

2727
::: 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.
2929
:::
3030

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:
3432

3533
```vue
3634
<script setup lang="ts">
3735
import type { Option } from "vue3-select-component";
3836
import { ref } from "vue";
3937
import VueSelect from "vue3-select-component";
4038
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`.
39+
// Specify number as the value type
4440
type UserOption = Option<number>;
4541
4642
const selectedUser = ref<number>();
@@ -49,7 +45,7 @@ const userOptions: UserOption[] = [
4945
{ label: "Alice", value: 1 },
5046
{ label: "Bob", value: 2 },
5147
{ label: "Charlie", value: 3 },
52-
// ❌ - This will cause a type error because `value` is not a number
48+
// ❌ - Type error: string is not assignable to number
5349
{ label: "David", value: "a string" },
5450
];
5551
</script>
@@ -63,19 +59,17 @@ const userOptions: UserOption[] = [
6359
</template>
6460
```
6561

66-
## Adding properties to `option`
67-
68-
It is possible to **add properties** to the options, while still being type-safe across the `<slot />` and various props.
62+
## Extending option properties
6963

70-
New option properties will be available on **all available props and slots** that receive the `option` object.
64+
The `Option` type can be extended with additional properties while maintaining type safety throughout slots and props:
7165

7266
```vue
7367
<script setup lang="ts">
7468
import type { Option } from "vue3-select-component";
7569
import { ref } from "vue";
7670
import VueSelect from "vue3-select-component";
7771
78-
// Define a custom type for the option value with an additional `username` property.
72+
// Extend Option type with username
7973
type UserOption = Option<number> & { username: string };
8074
8175
const selectedUser = ref<number>();
@@ -89,26 +83,22 @@ const userOptions: UserOption[] = [
8983
</script>
9084
9185
<template>
92-
<!-- The username property will be available on functions inside the VueSelect component. -->
9386
<VueSelect
9487
v-model="selectedUser"
9588
:options="userOptions"
9689
:get-option-label="option => `${option.label} (${option.username})`"
9790
placeholder="Pick a user"
9891
>
99-
<!-- The username property is also available on slots that receive an option object. -->
10092
<template #option="{ option }">
10193
<span>{{ option.label }} - {{ option.username }}</span>
10294
</template>
10395
</VueSelect>
10496
</template>
10597
```
10698

107-
## Type-safety between `option.value` & `v-model`
108-
109-
Vue 3 Select Component creates a type-safe relationship between the `option.value` and the `v-model` prop.
99+
## `v-model` type validation
110100

111-
This means that if you have a custom type for the `value` property of the option object, the `v-model` prop will also be type-safe.
101+
The component enforces type consistency between `option.value` and `v-model`:
112102

113103
```vue
114104
<script setup lang="ts">
@@ -118,8 +108,7 @@ import VueSelect from "vue3-select-component";
118108
119109
type UserOption = Option<number>;
120110
121-
// This `ref()` type implementation is incorrect, as it should
122-
// be `ref<number>()`.
111+
// ❌ Incorrect type - should be ref<number>()
123112
const selectedUser = ref<string>();
124113
125114
const userOptions: UserOption[] = [
@@ -130,10 +119,7 @@ const userOptions: UserOption[] = [
130119
</script>
131120
132121
<template>
133-
<!--
134-
Our v-model will cause a type error because `ref<string>()`
135-
cannot be assigned to `Option.value<number>`.
136-
-->
122+
<!-- Type error: string type conflicts with number type -->
137123
<VueSelect
138124
v-model="selectedUser"
139125
:options="userOptions"
@@ -142,26 +128,25 @@ const userOptions: UserOption[] = [
142128
</template>
143129
```
144130

145-
## Using custom label/value with options
131+
## Custom value mapping
146132

147133
::: warning
148-
`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.
149135
:::
150136

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:
152138

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
155141

156-
Here's an example usage of the `getOptionValue` and `getOptionLabel` props with TypeScript:
142+
Example implementation:
157143

158144
```vue
159145
<script setup lang="ts">
160146
import type { Option } from "vue3-select-component";
161147
162148
const activeRole = ref<string>("");
163149
164-
// You cannot type the `roleOptions` as `Option<string>[]`.
165150
const roleOptions = [
166151
{ id: "Admin", key: "admin" },
167152
{ id: "User", key: "user" },
@@ -170,7 +155,6 @@ const roleOptions = [
170155
</script>
171156
172157
<template>
173-
<!-- Casting of the `roleOptions` must be done at the `:options` prop-level. -->
174158
<VueSelect
175159
v-model="activeRole"
176160
:options="(roleOptions as unknown as Option<string>[])"

0 commit comments

Comments
 (0)