Skip to content

Commit 985b5a0

Browse files
committed
Add Vue augmentation to type global mixin properties
1 parent 21945e4 commit 985b5a0

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

.changeset/cool-trains-roll.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@vintl/vintl': minor
3+
---
4+
5+
Added Vue types augmentation to type the global mixin properties
6+
7+
Mixin-provided global properties like `$t`, `$fmt` and `$i18n` should be properly typed now.
8+
9+
If you don't use the global mixin, the below augmentation will remove these unusable properties.
10+
11+
```ts
12+
declare global {
13+
namespace VueIntlController {
14+
interface Options {
15+
globalMixin: false
16+
}
17+
}
18+
}
19+
```

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ You can declare your messages by creating an ambient declaration file where you
374374

375375
A map of locale resources provided by the load event listeners.
376376

377+
- `interface` `Options`
378+
379+
A map of options affecting type augmentation.
380+
381+
Currently supported options:
382+
383+
- `globalMixin` (`boolean`, default `true`) - whether to augment Vue types to type mixin-provided global properties.
384+
377385
<details>
378386
<summary>Example</summary>
379387

@@ -394,6 +402,10 @@ import type { ExampleObject } from '~/utils/convertibleObject.ts'
394402

395403
declare global {
396404
namespace VueIntlController {
405+
interface Options {
406+
globalMixin: false // Remove types for mixin-provided properties.
407+
}
408+
397409
interface MessageValueTypes {
398410
// key doesn't matter as long as it does not collide with other key;
399411
// the interface used here solely for extensibility since you cannot

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './vue-augment.js'
12
export * from './messages.js'
23
export * from './locales.js'
34
export * from './sources.js'

src/types/vue-augment.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { InjectedProperties } from '../plugin.js'
2+
import type { MessageValueType } from './messages.js'
3+
4+
declare global {
5+
namespace VueIntlController {
6+
interface Options {}
7+
}
8+
}
9+
10+
type ConditionalExtensions = VueIntlController.Options extends {
11+
globalMixin: infer GlobalMixin
12+
}
13+
? [GlobalMixin] extends [true]
14+
? InjectedProperties<MessageValueType>
15+
: {}
16+
: InjectedProperties<MessageValueType>
17+
18+
declare module 'vue' {
19+
export interface ComponentCustomProperties extends ConditionalExtensions {}
20+
}
21+
22+
export {}

typings/globals.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)