Skip to content

Commit a361a96

Browse files
authored
Merge branch 'cdmoro:main' into main
2 parents d509874 + 4bd4bee commit a361a96

File tree

7 files changed

+100
-42
lines changed

7 files changed

+100
-42
lines changed

apps/docs/docs/reference/types.md

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

33
/**
44
* @external
55
*/
6-
type ButtonVariant =
7-
| ColorVariant
8-
| 'link'
9-
| 'outline-primary'
10-
| 'outline-secondary'
11-
| 'outline-success'
12-
| 'outline-danger'
13-
| 'outline-warning'
14-
| 'outline-info'
15-
| 'outline-light'
16-
| 'outline-dark'
6+
export interface BaseButtonVariant extends BaseColorVariant {
7+
'link': unknown
8+
'outline-primary': unknown
9+
'outline-secondary': unknown
10+
'outline-success': unknown
11+
'outline-danger': unknown
12+
'outline-warning': unknown
13+
'outline-info': unknown
14+
'outline-light': unknown
15+
'outline-dark': unknown
16+
}
17+
18+
type ButtonVariant = keyof BaseButtonVariant
1719

1820
export default ButtonVariant
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/**
22
* @external
33
*/
4-
type ColorVariant =
5-
| 'primary'
6-
| 'secondary'
7-
| 'success'
8-
| 'danger'
9-
| 'warning'
10-
| 'info'
11-
| 'light'
12-
| 'dark'
4+
export interface BaseColorVariant {
5+
'primary': unknown
6+
'secondary': unknown
7+
'success': unknown
8+
'danger': unknown
9+
'warning': unknown
10+
'info': unknown
11+
'light': unknown
12+
'dark': unknown
13+
}
14+
15+
type ColorVariant = keyof BaseColorVariant
1316

1417
export default ColorVariant
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/**
22
* @external
33
*/
4-
type InputSize = 'sm' | 'md' | 'lg'
4+
export interface BaseInputSize {
5+
'sm': unknown,
6+
'md': unknown,
7+
'lg': unknown,
8+
}
9+
10+
type InputSize = keyof BaseInputSize
511

612
export default InputSize
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/**
22
* @external
33
*/
4-
type Size = 'sm' | 'lg' | undefined
4+
export interface BaseSize {
5+
'sm': unknown,
6+
'lg': unknown,
7+
}
8+
9+
type Size = keyof BaseSize | undefined
510

611
export default Size
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
import type {BaseColorVariant} from '.'
2+
13
/**
24
* @external
35
*/
4-
type TextColorVariant =
5-
| 'primary'
6-
| 'secondary'
7-
| 'success'
8-
| 'danger'
9-
| 'warning'
10-
| 'info'
11-
| 'light'
12-
| 'dark'
13-
| 'white'
14-
| 'body'
15-
| 'muted'
16-
| 'black-50'
17-
| 'white-50'
18-
| 'reset'
6+
export interface BaseTextColorVariant extends BaseColorVariant {
7+
'white': unknown
8+
'body': unknown
9+
'muted': unknown
10+
'black-50': unknown
11+
'white-50': unknown
12+
'reset': unknown
13+
}
14+
15+
type TextColorVariant = keyof BaseTextColorVariant
1916

2017
export default TextColorVariant

packages/bootstrap-vue-3/src/types/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export type {
1212
RowColsBreakpointProps,
1313
} from './BreakpointProps'
1414
export type {default as ButtonType} from './ButtonType'
15-
export type {default as ButtonVariant} from './ButtonVariant'
15+
export type {default as ButtonVariant, BaseButtonVariant} from './ButtonVariant'
1616
export type {default as ClassValue} from './ClassValue'
17-
export type {default as ColorVariant} from './ColorVariant'
17+
export type {default as ColorVariant, BaseColorVariant} from './ColorVariant'
1818
export type {ContainerHorizontalAlign, ContainerPosition, ContainerVerticalAlign} from './Container'
1919
export type {default as InputSize} from './InputSize'
2020
export type {default as InputType} from './InputType'
@@ -27,12 +27,12 @@ export type {default as PlaceholderSize} from './PlaceholderSize'
2727
export type {default as Position} from './Position'
2828
export type {default as RadioOption} from './RadioOption'
2929
export type {default as SelectOption} from './SelectOption'
30-
export type {default as Size} from './Size'
30+
export type {default as Size, BaseSize} from './Size'
3131
export type {default as SkeletonAnimation} from './SkeletonAnimation'
3232
export type {default as SkeletonType} from './SkeletonType'
3333
export type {default as SpinnerType} from './SpinnerType'
3434
export type {default as TableField, TableFieldObject} from './TableField'
3535
export type {default as TableItem} from './TableItem'
36-
export type {default as TextColorVariant} from './TextColorVariant'
36+
export type {default as TextColorVariant, BaseTextColorVariant} from './TextColorVariant'
3737
export type {default as TransitionMode} from './TransitionMode'
3838
export type {default as VerticalAlign} from './VerticalAlign'

0 commit comments

Comments
 (0)