Skip to content

Commit c8445db

Browse files
committed
feat(docs): Extending types
1 parent 2f0afd5 commit c8445db

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

apps/docs/docs/reference/types.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,48 @@ type TransitionMode = 'in-out' | 'out-in'
241241
```ts
242242
type VerticalAlign = 'baseline' | 'top' | 'middle' | 'bottom' | 'text-top' | 'text-bottom'
243243
```
244+
245+
## Extending types
246+
247+
You can extend some types to use your own values (e.g. colors, sizes). This requires the use of interface augmentation. You can augment next interfaces:
248+
249+
- BaseColorVariant
250+
- BaseButtonVariant (extends BaseColorVariant)
251+
- BaseTextColorVariant (extends BaseColorVariant)
252+
- BaseSize
253+
- BaseInputSize
254+
255+
Suppose we want to add a purple style and extra-large (xl) sizes.
256+
We need to create a declaration file in the root of vue project.
257+
258+
**shims-bootstrap-vue-3.d.ts**
259+
260+
```ts
261+
import 'bootstrap-vue-3'
262+
263+
declare module 'bootstrap-vue-3/dist/types' {
264+
export interface BaseColorVariant {
265+
'purple': unknown // we use unknown type because it does not matter here
266+
}
267+
export interface BaseButtonVariant {
268+
// there is no need to add "purple" (it inherits from BaseColorVariant)
269+
'outline-purple': unknown // outline purple button
270+
}
271+
export interface BaseTextColorVariant {
272+
// there is no need to add "purple" (it inherits from BaseColorVariant)
273+
}
274+
export interface BaseSize {
275+
'xl': unknown // extra large
276+
}
277+
export interface BaseInputSize {
278+
'xl': unknown // extra large (not inherits from BaseSize)
279+
}
280+
}
281+
```
282+
283+
New values can be used now and the type check will be successful:
284+
285+
```html
286+
<b-button variant="purple" size="xl">Extra large purple button</b-button>
287+
<b-button variant="outline-purple">Outline purple button</b-button>
288+
```

0 commit comments

Comments
 (0)