Skip to content

Commit

Permalink
feat: improve types (#47)
Browse files Browse the repository at this point in the history
* feat: provide vuex types

`$moment` is injected using nuxt `inject` method and is therefore also available in `vuex`.

* feat: Improve types

This makes all the ways to use moment type-safe, e.g. this is now type-safe:
`this.$moment.duration(this.$moment(moment1).diff(moment2)).humanize()`

* chore(readme): Add instructions for typescript setup

* chore(readme): Add more details to typescript setup

* fix(types): Use vuex/types/index

* chore(readme): Typescript setup for nuxt > 2.9
  • Loading branch information
P4sca1 authored and ricardogobbosouza committed Nov 14, 2019
1 parent eed4398 commit 6a26feb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -52,6 +52,28 @@ export default {
}
```

### Typescript setup

Add the types to your "types" array in tsconfig.json after the `@nuxt/types` entry.

:warning: Use `@nuxt/vue-app` instead of `@nuxt/types` for nuxt < 2.9.

**tsconfig.json**

```json
{
"compilerOptions": {
"types": [
"@nuxt/types",
"@nuxtjs/moment"
]
}
}
```
> **Why?**
>
> For typescript to be aware of the additions to the `nuxt Context`, the `vue instance` and the `vuex store`, the types need to be merged via [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) by adding `@nuxtjs/moment` to your types.
## Configuration

To strip all locales except `en`:
Expand Down
15 changes: 11 additions & 4 deletions types/index.d.ts
@@ -1,21 +1,28 @@
import { Moment, MomentFormatSpecification, MomentInput } from 'moment'
import moment from 'moment'
import Vue from 'vue'

declare module '@nuxt/vue-app' {
interface Context {
$moment(input?: MomentInput, format?: MomentFormatSpecification, language?: string, strict?: boolean): Moment
$moment: typeof moment
}
}

// Nuxt 2.9+
declare module '@nuxt/types' {
interface Context {
$moment(input?: MomentInput, format?: MomentFormatSpecification, language?: string, strict?: boolean): Moment
$moment: typeof moment
}
}

declare module 'vue/types/vue' {
interface Vue {
$moment(input?: MomentInput, format?: MomentFormatSpecification, language?: string, strict?: boolean): Moment
$moment: typeof moment
}
}

declare module 'vuex/types/index' {
interface Store<S> {
$moment: typeof moment
}
}

0 comments on commit 6a26feb

Please sign in to comment.