Skip to content

Commit b37ac83

Browse files
author
Yash Dayama
committed
fix(types): update Route query type definition to support null values
1 parent 786ed34 commit b37ac83

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

flow/declarations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ declare type Location = {
8484
name?: string;
8585
path?: string;
8686
hash?: string;
87-
query?: Dictionary<string>;
87+
query?: Dictionary<string | null | Array<string | null>>;
8888
params?: Dictionary<string>;
8989
append?: boolean;
9090
replace?: boolean;
@@ -96,7 +96,7 @@ declare type Route = {
9696
path: string;
9797
name: ?string;
9898
hash: string;
99-
query: Dictionary<string>;
99+
query: Dictionary<string | null | Array<string | null>>;
100100
params: Dictionary<string>;
101101
fullPath: string;
102102
matched: Array<RouteRecord>;

types/router.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export interface Route {
411411
path: string
412412
name?: string | null
413413
hash: string
414-
query: Dictionary<string | (string | null)[]>
414+
query: Dictionary<string | null | (string | null)[]>
415415
params: Dictionary<string>
416416
fullPath: string
417417
matched: RouteRecord[]

types/test/query-types.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import VueRouter from '../index'
2+
import { Route } from '../index'
3+
4+
const router = new VueRouter()
5+
const route: Route = router.currentRoute
6+
7+
const stringQuery: string | null | (string | null)[] = route.query['stringParam']
8+
if (typeof stringQuery === 'string') {
9+
console.log(stringQuery.toLowerCase())
10+
}
11+
12+
const nullQuery: string | null | (string | null)[] = route.query['paramWithoutValue']
13+
if (nullQuery === null) {
14+
console.log('Parameter exists but has no value')
15+
}
16+
17+
const arrayQuery: string | null | (string | null)[] = route.query['arrayParam']
18+
if (Array.isArray(arrayQuery)) {
19+
arrayQuery.forEach(value => {
20+
if (value === null) {
21+
console.log('Array contains null value')
22+
} else {
23+
console.log(value.toLowerCase())
24+
}
25+
})
26+
}
27+
28+
router.push({
29+
path: '/test',
30+
query: {
31+
string: 'value', // string value
32+
empty: null, // null value
33+
array: ['value1', 'value2'], // array of strings
34+
mixedArray: ['value', null], // array with null
35+
noValue: null // parameter without value
36+
}
37+
})
38+
39+
const routeWithQueries: Route = {
40+
path: '/test',
41+
name: null,
42+
hash: '',
43+
query: {
44+
param1: 'string', // string value
45+
param2: null, // null value
46+
param3: ['val1', 'val2'], // array of strings
47+
param4: ['val', null], // array with null
48+
param5: null // parameter without value
49+
},
50+
params: {},
51+
fullPath: '/test',
52+
matched: []
53+
}

0 commit comments

Comments
 (0)