Skip to content

Commit

Permalink
fix(types): append declare module
Browse files Browse the repository at this point in the history
Close #419
  • Loading branch information
posva committed Aug 12, 2020
1 parent 354ba3f commit 50ad404
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"scripts": {
"build": "rollup -c rollup.config.js",
"build:dts": "api-extractor run --local --verbose",
"build:dts": "api-extractor run --local --verbose && tail -n +7 src/globalExtensions.ts >> dist/vue-router.d.ts",
"dev": "webpack-dev-server --mode=development",
"release": "bash scripts/release.sh",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
Expand Down
1 change: 1 addition & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ then
rm -rf node_modules/.rts2_cache
yarn run build
yarn run build:dts
yarn run test:dts

# generate the version so that the changelog can be generated too
yarn version --no-git-tag-version --no-commit-hooks --new-version $VERSION
Expand Down
58 changes: 58 additions & 0 deletions src/globalExtensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
NavigationGuardWithThis,
NavigationGuard,
RouteLocationNormalizedLoaded,
} from './types'
import { Router } from './router'

declare module 'vue' {
export interface ComponentCustomOptions {
/**
* Guard called when the router is navigating to the route that is rendering
* this component from a different route. Differently from `beforeRouteUpdate`
* and `beforeRouteLeave`, `beforeRouteEnter` does not have access to the
* component instance through `this` because it triggers before the component
* is even mounted.
*
* @param to - RouteLocationRaw we are navigating to
* @param from - RouteLocationRaw we are navigating from
* @param next - function to validate, cancel or modify (by redirecting) the
* navigation
*/
beforeRouteEnter?: NavigationGuardWithThis<undefined>

/**
* Guard called whenever the route that renders this component has changed but
* it is reused for the new route. This allows you to guard for changes in
* params, the query or the hash.
*
* @param to - RouteLocationRaw we are navigating to
* @param from - RouteLocationRaw we are navigating from
* @param next - function to validate, cancel or modify (by redirecting) the
* navigation
*/
beforeRouteUpdate?: NavigationGuard

/**
* Guard called when the router is navigating away from the current route that
* is rendering this component.
*
* @param to - RouteLocationRaw we are navigating to
* @param from - RouteLocationRaw we are navigating from
* @param next - function to validate, cancel or modify (by redirecting) the
* navigation
*/
beforeRouteLeave?: NavigationGuard
}

export interface ComponentCustomProperties {
/**
* Normalized current location. See {@link RouteLocationNormalizedLoaded}.
*/
$route: RouteLocationNormalizedLoaded
/**
* {@link Router} instance used by the application.
*/
$router: Router
}
}
59 changes: 1 addition & 58 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import {
NavigationGuard,
RouteLocationNormalizedLoaded,
NavigationGuardWithThis,
} from './types'
import { Router } from './router'

export { createWebHistory } from './history/html5'
export { createMemoryHistory } from './history/memory'
export { createWebHashHistory } from './history/hash'
Expand Down Expand Up @@ -62,54 +55,4 @@ export { RouterView, RouterViewProps } from './RouterView'

export * from './useApi'

declare module '@vue/runtime-core' {
export interface ComponentCustomOptions {
/**
* Guard called when the router is navigating to the route that is rendering
* this component from a different route. Differently from `beforeRouteUpdate`
* and `beforeRouteLeave`, `beforeRouteEnter` does not have access to the
* component instance through `this` because it triggers before the component
* is even mounted.
*
* @param to - RouteLocationRaw we are navigating to
* @param from - RouteLocationRaw we are navigating from
* @param next - function to validate, cancel or modify (by redirecting) the
* navigation
*/
beforeRouteEnter?: NavigationGuardWithThis<undefined>

/**
* Guard called whenever the route that renders this component has changed but
* it is reused for the new route. This allows you to guard for changes in
* params, the query or the hash.
*
* @param to - RouteLocationRaw we are navigating to
* @param from - RouteLocationRaw we are navigating from
* @param next - function to validate, cancel or modify (by redirecting) the
* navigation
*/
beforeRouteUpdate?: NavigationGuard

/**
* Guard called when the router is navigating away from the current route that
* is rendering this component.
*
* @param to - RouteLocationRaw we are navigating to
* @param from - RouteLocationRaw we are navigating from
* @param next - function to validate, cancel or modify (by redirecting) the
* navigation
*/
beforeRouteLeave?: NavigationGuard
}

export interface ComponentCustomProperties {
/**
* Normalized current location. See {@link RouteLocationNormalizedLoaded}.
*/
$route: RouteLocationNormalizedLoaded
/**
* {@link Router} instance used by the application.
*/
$router: Router
}
}
export * from './globalExtensions'
11 changes: 11 additions & 0 deletions test-dts/legacy.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Router, RouteLocationNormalizedLoaded, expectType } from './index'
import { defineComponent } from 'vue'

defineComponent({
methods: {
doStuff() {
expectType<Router>(this.$router)
expectType<RouteLocationNormalizedLoaded>(this.$route)
},
},
})

0 comments on commit 50ad404

Please sign in to comment.