Skip to content

Commit

Permalink
fix: allow extension of useContext return type
Browse files Browse the repository at this point in the history
closes #84 - fix: `useContext` return type is not extensible
  • Loading branch information
danielroe committed May 30, 2020
1 parent 1aeabc4 commit 24d7586
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getCurrentInstance, computed } from '@vue/composition-api'

import type { Ref } from '@vue/composition-api'
import type { Context } from '@nuxt/types'
import type { Route } from 'vue-router'

interface ContextCallback {
(context: Context): void
Expand All @@ -17,8 +19,16 @@ export const withContext = (callback: ContextCallback) => {
callback(vm['<%= options.globalNuxt %>' as '$nuxt'].context)
}

interface UseContextReturn
extends Omit<Context, 'route' | 'query' | 'from' | 'params'> {
route: Ref<Route>
query: Ref<Route['query']>
from: Ref<Route['redirectedFrom']>
params: Ref<Route['params']>
}

/**
* `useContext` which will return the Nuxt context.
* `useContext` will return the Nuxt context.
* @example
```ts
import { defineComponent, ref, useContext } from 'nuxt-composition-api'
Expand All @@ -31,7 +41,7 @@ export const withContext = (callback: ContextCallback) => {
})
```
*/
export const useContext = () => {
export const useContext = (): UseContextReturn => {
const vm = getCurrentInstance()
if (!vm) throw new Error('This must be called within a setup function.')

Expand Down

0 comments on commit 24d7586

Please sign in to comment.