]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(types): append declare module
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 12 Aug 2020 08:02:28 +0000 (10:02 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 12 Aug 2020 08:02:28 +0000 (10:02 +0200)
Close #419

package.json
scripts/release.sh
src/globalExtensions.ts [new file with mode: 0644]
src/index.ts
test-dts/legacy.test-d.ts [new file with mode: 0644]

index bb06d2fc6c9297b70a75fd17d8a0821d33b1f881..d8a01e83e646c1363d64e746ff11a890c4882b4b 100644 (file)
@@ -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",
index 7b4e3e487e7ae0d897657a5d1da1c5b67279e823..7a973853626563c71008f5f5818a1189f7f017eb 100644 (file)
@@ -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
diff --git a/src/globalExtensions.ts b/src/globalExtensions.ts
new file mode 100644 (file)
index 0000000..d903b6e
--- /dev/null
@@ -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
+  }
+}
index 90b46d2e1649e4290cff88ec50bb854b5b715f57..a4bd08a3a5d46459e46e9f4b972b9ff594c470c0 100644 (file)
@@ -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'
@@ -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'
diff --git a/test-dts/legacy.test-d.ts b/test-dts/legacy.test-d.ts
new file mode 100644 (file)
index 0000000..cd28179
--- /dev/null
@@ -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)
+    },
+  },
+})