diff --git a/flow/declarations.js b/flow/declarations.js index e10959cb1..6bbdbec34 100644 --- a/flow/declarations.js +++ b/flow/declarations.js @@ -5,53 +5,61 @@ declare module 'path-to-regexp' { } } +declare type Dictionary = { [key: string]: T } + declare type RouterOptions = { routes?: Array; mode?: string; base?: string; linkActiveClass?: string; - scrollBehavior?: Function; + scrollBehavior?: ( + to: Route, + from: Route, + savedPosition: ?{ x: number, y: number } + ) => { x: number, y: number } | { selector: string } | ?{}; } -declare type RedirectOption = RawLocation | Function +declare type RedirectOption = RawLocation | ((to: Route) => RawLocation) declare type RouteConfig = { path: string; name?: string; component?: any; - components?: { [name: string]: any }; + components?: Dictionary; redirect?: RedirectOption; alias?: string | Array; children?: Array; - beforeEnter?: Function; + beforeEnter?: ( + route: Route, + redirect: (location: RawLocation) => void, + next: () => void + ) => any; meta?: any; } declare type RouteRecord = { path: string; - components: { [name: string]: any }; - instances: { [name: string]: any }; + components: Dictionary; + instances: Dictionary; name: ?string; parent: ?RouteRecord; redirect: ?RedirectOption; matchAs: ?string; - beforeEnter: ?Function; + beforeEnter: ?( + route: Route, + redirect: (location: RawLocation) => void, + next: () => void + ) => any; meta: any; } -declare type RouteMap = { - [key: string]: RouteRecord; -} - -declare type StringHash = { [key: string]: string } - declare type Location = { _normalized?: boolean; name?: string; path?: string; hash?: string; - query?: StringHash; - params?: StringHash; + query?: Dictionary; + params?: Dictionary; } declare type RawLocation = string | Location @@ -60,8 +68,8 @@ declare type Route = { path: string; name: ?string; hash: string; - query: StringHash; - params: StringHash; + query: Dictionary; + params: Dictionary; fullPath: string; matched: Array; redirectedFrom?: string; diff --git a/src/create-route-map.js b/src/create-route-map.js index 45cecf538..3b1f4d65e 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -4,11 +4,11 @@ import { assert, warn } from './util/warn' import { cleanPath } from './util/path' export function createRouteMap (routes: Array): { - pathMap: RouteMap, - nameMap: RouteMap + pathMap: Dictionary, + nameMap: Dictionary } { - const pathMap: RouteMap = Object.create(null) - const nameMap: RouteMap = Object.create(null) + const pathMap: Dictionary = Object.create(null) + const nameMap: Dictionary = Object.create(null) routes.forEach(route => { addRouteRecord(pathMap, nameMap, route) @@ -21,8 +21,8 @@ export function createRouteMap (routes: Array): { } function addRouteRecord ( - pathMap: RouteMap, - nameMap: RouteMap, + pathMap: Dictionary, + nameMap: Dictionary, route: RouteConfig, parent?: RouteRecord, matchAs?: string diff --git a/src/history/html5.js b/src/history/html5.js index 09f6f02d5..6954c5ac7 100644 --- a/src/history/html5.js +++ b/src/history/html5.js @@ -86,7 +86,7 @@ export class HTML5History extends History { return } const isObject = typeof shouldScroll === 'object' - if (isObject && shouldScroll.selector) { + if (isObject && typeof shouldScroll.selector === 'string') { const el = document.querySelector(shouldScroll.selector) if (el) { position = getElementPosition(el) diff --git a/src/util/query.js b/src/util/query.js index 9e7474503..8a6d158a3 100644 --- a/src/util/query.js +++ b/src/util/query.js @@ -7,8 +7,8 @@ const decode = decodeURIComponent export function resolveQuery ( query: ?string, - extraQuery: StringHash = {} -): StringHash { + extraQuery: Dictionary = {} +): Dictionary { if (query) { let parsedQuery try { @@ -26,7 +26,7 @@ export function resolveQuery ( } } -function parseQuery (query: string): StringHash { +function parseQuery (query: string): Dictionary { const res = Object.create(null) query = query.trim().replace(/^(\?|#|&)/, '') @@ -54,7 +54,7 @@ function parseQuery (query: string): StringHash { return res } -export function stringifyQuery (obj: StringHash): string { +export function stringifyQuery (obj: Dictionary): string { const res = obj ? Object.keys(obj).sort().map(key => { const val = obj[key] diff --git a/src/util/route.js b/src/util/route.js index 9359f2150..412ec61ce 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -75,7 +75,7 @@ export function isIncludedRoute (current: Route, target: Route): boolean { ) } -function queryIncludes (current: StringHash, target: StringHash): boolean { +function queryIncludes (current: Dictionary, target: Dictionary): boolean { for (const key in target) { if (!(key in current)) { return false