|
| 1 | +--- |
| 2 | +title: 'TypeScript' |
| 3 | +--- |
| 4 | + |
| 5 | +# TypeScript |
| 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. |
| 8 | + |
| 9 | +## About generics in TypeScript & Vue |
| 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). |
| 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. |
| 14 | + |
| 15 | +A common type you'll see is the `Option` type, which is used to define the options of the select component. |
| 16 | + |
| 17 | +```typescript |
| 18 | +type Option<T> = { |
| 19 | + label: string; |
| 20 | + value: T; |
| 21 | +}; |
| 22 | +``` |
| 23 | + |
| 24 | +## Custom option value |
| 25 | + |
| 26 | +::: info |
| 27 | +Ensure you are familiar with the [`:options` prop](/props#options) before reading this section. |
| 28 | +::: |
| 29 | + |
| 30 | +By default, the `value` property of the option object is a `string`. However, it is possible to use a custom type, such as a `number` or a complex object. |
| 31 | + |
| 32 | +```vue |
| 33 | +<script setup lang="ts"> |
| 34 | +import { ref } from "vue"; |
| 35 | +import VueSelect, { type Option } from "vue3-select-component"; |
| 36 | +
|
| 37 | +// Define a custom type for the option value. |
| 38 | +// It takes a generic type that defines the type of the `value` property. |
| 39 | +// In this case, the `value` property is a `number`. |
| 40 | +type UserOption = Option<number>; |
| 41 | +
|
| 42 | +const selectedUser = ref<number>(); |
| 43 | +
|
| 44 | +const userOptions: UserOption[] = [ |
| 45 | + { label: "Alice", value: 1 }, |
| 46 | + { label: "Bob", value: 2 }, |
| 47 | + { label: "Charlie", value: 3 }, |
| 48 | + // ❌ - This will cause a type error because `value` is not a number |
| 49 | + { label: "David", value: "a string" }, |
| 50 | +]; |
| 51 | +</script> |
| 52 | +
|
| 53 | +<template> |
| 54 | + <VueSelect |
| 55 | + v-model="selectedUser" |
| 56 | + :options="userOptions" |
| 57 | + placeholder="Pick a user" |
| 58 | + /> |
| 59 | +</template> |
| 60 | +``` |
| 61 | + |
| 62 | +## Custom option properties |
| 63 | + |
| 64 | +It is possible to **add properties** to the options passed inside the `:options` prop, while still being type-safe. |
| 65 | + |
| 66 | +Let's say you want to add a `username` property to the option object. |
| 67 | + |
| 68 | +This `username` property will be available on **all available props and slots** that receive the `option` object. |
| 69 | + |
| 70 | +```vue |
| 71 | +<script setup lang="ts"> |
| 72 | +import { ref } from "vue"; |
| 73 | +import VueSelect, { type Option } from "vue3-select-component"; |
| 74 | +
|
| 75 | +type UserOption = Option<number> & { username: string }; |
| 76 | +
|
| 77 | +const selectedUser = ref<number>(); |
| 78 | +
|
| 79 | +const userOptions: UserOption[] = [ |
| 80 | + { label: "Alice", value: 1, username: "alice15" }, |
| 81 | + { label: "Bob", value: 2, username: "bob01" }, |
| 82 | + { label: "Charlie", value: 3, username: "charlie20" }, |
| 83 | + { label: "David", value: 4, username: "david30" }, |
| 84 | +]; |
| 85 | +</script> |
| 86 | +
|
| 87 | +<template> |
| 88 | + <!-- The username property will be available on functions inside the VueSelect component. --> |
| 89 | + <VueSelect |
| 90 | + v-model="selectedUser" |
| 91 | + :options="userOptions" |
| 92 | + :get-option-label="option => `${option.label} (${option.username})`" |
| 93 | + placeholder="Pick a user" |
| 94 | + > |
| 95 | + <!-- The username property is also available on slots that receive an option object. --> |
| 96 | + <template #option="{ option }"> |
| 97 | + <span>{{ option.label }} - {{ option.username }}</span> |
| 98 | + </template> |
| 99 | + </VueSelect> |
| 100 | +</template> |
| 101 | +``` |
| 102 | + |
| 103 | +## Type-safe relationship between `option.value` & `v-model` |
| 104 | + |
| 105 | +Vue 3 Select Component creates a type-safe relationship between the `option.value` and the `v-model` prop. |
| 106 | + |
| 107 | +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. |
| 108 | + |
| 109 | +```vue |
| 110 | +<script setup lang="ts"> |
| 111 | +import { ref } from "vue"; |
| 112 | +import VueSelect, { type Option } from "vue3-select-component"; |
| 113 | +
|
| 114 | +type UserOption = Option<number>; |
| 115 | +
|
| 116 | +// This `ref()` type implementation is incorrect, as it should |
| 117 | +// be `ref<number>()`. |
| 118 | +const selectedUser = ref<string>(); |
| 119 | +
|
| 120 | +const userOptions: UserOption[] = [ |
| 121 | + { label: "Alice", value: 1 }, |
| 122 | + { label: "Bob", value: 2 }, |
| 123 | + { label: "Charlie", value: 3 }, |
| 124 | +]; |
| 125 | +</script> |
| 126 | +
|
| 127 | +<template> |
| 128 | + <!-- |
| 129 | + Our v-model will cause a type error because `ref<string>()` |
| 130 | + cannot be assigned to `Option.value<number>`. |
| 131 | + --> |
| 132 | + <VueSelect |
| 133 | + v-model="selectedUser" |
| 134 | + :options="userOptions" |
| 135 | + placeholder="Pick a user" |
| 136 | + /> |
| 137 | +</template> |
| 138 | +``` |
0 commit comments