Skip to content

Commit 50ad404

Browse files
committed
fix(types): append declare module
Close vuejs#419
1 parent 354ba3f commit 50ad404

File tree

5 files changed

+72
-59
lines changed

5 files changed

+72
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"scripts": {
2020
"build": "rollup -c rollup.config.js",
21-
"build:dts": "api-extractor run --local --verbose",
21+
"build:dts": "api-extractor run --local --verbose && tail -n +7 src/globalExtensions.ts >> dist/vue-router.d.ts",
2222
"dev": "webpack-dev-server --mode=development",
2323
"release": "bash scripts/release.sh",
2424
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",

scripts/release.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ then
1515
rm -rf node_modules/.rts2_cache
1616
yarn run build
1717
yarn run build:dts
18+
yarn run test:dts
1819

1920
# generate the version so that the changelog can be generated too
2021
yarn version --no-git-tag-version --no-commit-hooks --new-version $VERSION

src/globalExtensions.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {
2+
NavigationGuardWithThis,
3+
NavigationGuard,
4+
RouteLocationNormalizedLoaded,
5+
} from './types'
6+
import { Router } from './router'
7+
8+
declare module 'vue' {
9+
export interface ComponentCustomOptions {
10+
/**
11+
* Guard called when the router is navigating to the route that is rendering
12+
* this component from a different route. Differently from `beforeRouteUpdate`
13+
* and `beforeRouteLeave`, `beforeRouteEnter` does not have access to the
14+
* component instance through `this` because it triggers before the component
15+
* is even mounted.
16+
*
17+
* @param to - RouteLocationRaw we are navigating to
18+
* @param from - RouteLocationRaw we are navigating from
19+
* @param next - function to validate, cancel or modify (by redirecting) the
20+
* navigation
21+
*/
22+
beforeRouteEnter?: NavigationGuardWithThis<undefined>
23+
24+
/**
25+
* Guard called whenever the route that renders this component has changed but
26+
* it is reused for the new route. This allows you to guard for changes in
27+
* params, the query or the hash.
28+
*
29+
* @param to - RouteLocationRaw we are navigating to
30+
* @param from - RouteLocationRaw we are navigating from
31+
* @param next - function to validate, cancel or modify (by redirecting) the
32+
* navigation
33+
*/
34+
beforeRouteUpdate?: NavigationGuard
35+
36+
/**
37+
* Guard called when the router is navigating away from the current route that
38+
* is rendering this component.
39+
*
40+
* @param to - RouteLocationRaw we are navigating to
41+
* @param from - RouteLocationRaw we are navigating from
42+
* @param next - function to validate, cancel or modify (by redirecting) the
43+
* navigation
44+
*/
45+
beforeRouteLeave?: NavigationGuard
46+
}
47+
48+
export interface ComponentCustomProperties {
49+
/**
50+
* Normalized current ___location. See {@link RouteLocationNormalizedLoaded}.
51+
*/
52+
$route: RouteLocationNormalizedLoaded
53+
/**
54+
* {@link Router} instance used by the application.
55+
*/
56+
$router: Router
57+
}
58+
}

src/index.ts

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import {
2-
NavigationGuard,
3-
RouteLocationNormalizedLoaded,
4-
NavigationGuardWithThis,
5-
} from './types'
6-
import { Router } from './router'
7-
81
export { createWebHistory } from './history/html5'
92
export { createMemoryHistory } from './history/memory'
103
export { createWebHashHistory } from './history/hash'
@@ -62,54 +55,4 @@ export { RouterView, RouterViewProps } from './RouterView'
6255

6356
export * from './useApi'
6457

65-
declare module '@vue/runtime-core' {
66-
export interface ComponentCustomOptions {
67-
/**
68-
* Guard called when the router is navigating to the route that is rendering
69-
* this component from a different route. Differently from `beforeRouteUpdate`
70-
* and `beforeRouteLeave`, `beforeRouteEnter` does not have access to the
71-
* component instance through `this` because it triggers before the component
72-
* is even mounted.
73-
*
74-
* @param to - RouteLocationRaw we are navigating to
75-
* @param from - RouteLocationRaw we are navigating from
76-
* @param next - function to validate, cancel or modify (by redirecting) the
77-
* navigation
78-
*/
79-
beforeRouteEnter?: NavigationGuardWithThis<undefined>
80-
81-
/**
82-
* Guard called whenever the route that renders this component has changed but
83-
* it is reused for the new route. This allows you to guard for changes in
84-
* params, the query or the hash.
85-
*
86-
* @param to - RouteLocationRaw we are navigating to
87-
* @param from - RouteLocationRaw we are navigating from
88-
* @param next - function to validate, cancel or modify (by redirecting) the
89-
* navigation
90-
*/
91-
beforeRouteUpdate?: NavigationGuard
92-
93-
/**
94-
* Guard called when the router is navigating away from the current route that
95-
* is rendering this component.
96-
*
97-
* @param to - RouteLocationRaw we are navigating to
98-
* @param from - RouteLocationRaw we are navigating from
99-
* @param next - function to validate, cancel or modify (by redirecting) the
100-
* navigation
101-
*/
102-
beforeRouteLeave?: NavigationGuard
103-
}
104-
105-
export interface ComponentCustomProperties {
106-
/**
107-
* Normalized current ___location. See {@link RouteLocationNormalizedLoaded}.
108-
*/
109-
$route: RouteLocationNormalizedLoaded
110-
/**
111-
* {@link Router} instance used by the application.
112-
*/
113-
$router: Router
114-
}
115-
}
58+
export * from './globalExtensions'

test-dts/legacy.test-d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Router, RouteLocationNormalizedLoaded, expectType } from './index'
2+
import { defineComponent } from 'vue'
3+
4+
defineComponent({
5+
methods: {
6+
doStuff() {
7+
expectType<Router>(this.$router)
8+
expectType<RouteLocationNormalizedLoaded>(this.$route)
9+
},
10+
},
11+
})

0 commit comments

Comments
 (0)