feat: strict type for definePage param default · vuejs/router@0ae10cd (original) (raw)
`@@ -100,28 +100,96 @@ export interface DefinePage extends Partial<
`
100
100
`/**
`
101
101
` * Parameters extracted from the query.
`
102
102
` */
`
103
``
`-
query?: Record<string, DefinePageQueryParamOptions | ParamParserType>
`
``
103
`+
query?: Record<string, DefinePageQueryParamOptionsAny | ParamParserType>
`
104
104
`}
`
105
105
`}
`
106
106
``
107
``
`-
export type ParamParserType_Native = 'int' | 'bool'
`
``
107
`+
/**
`
``
108
`+
- Built-in param parsers. Always available; merged into {@link ParamParsers}
`
``
109
`+
- alongside any entries the user augments into {@link TypesConfig.ParamParsers}.
`
``
110
`+
`
``
111
`+
- @internal
`
``
112
`+
*/
`
``
113
`+
export interface ParamParsers_Native {
`
``
114
`+
int: { type: number }
`
``
115
`+
bool: { type: boolean }
`
``
116
`+
}
`
``
117
+
``
118
`+
export type ParamParserType_Native = keyof ParamParsers_Native
`
108
119
``
109
``
`-
export type ParamParserType =
`
110
``
`-
| (TypesConfig extends Record<'ParamParsers', infer ParamParsers>
`
111
``
`-
? ParamParsers
`
112
``
`-
: never)
`
113
``
`-
| ParamParserType_Native
`
``
120
`+
/**
`
``
121
`+
- Full registry of param parsers: built-ins merged with whatever the user
`
``
122
`+
- augments into {@link TypesConfig.ParamParsers}. Each entry is shaped
`
``
123
`` +
{ type: T }so the parsed value type can be looked up by name via
``
``
124
`+
- {@link ParamParserTypeOf}. The vue-router codegen emits this augmentation
`
``
125
`` +
- automatically from files in the
params/folder.
``
``
126
`+
`
``
127
`+
- @internal
`
``
128
`+
*/
`
``
129
`+
export type ParamParsers = ParamParsers_Native &
`
``
130
`+
(TypesConfig extends { _ParamParsers: infer P } ? P : {})
`
``
131
+
``
132
`+
/**
`
``
133
`+
- Union of all known parser names (built-in + augmented).
`
``
134
`+
*/
`
``
135
`+
export type ParamParserType = keyof ParamParsers
`
``
136
+
``
137
`+
/**
`
``
138
`+
- Resolves the parsed value type for a given parser name. Distributes over a
`
``
139
`` +
- union of names, so
ParamParserTypeOf<'int' | 'date'>→number | Date.
``
``
140
`+
`
``
141
`` +
- Falls back to
unknownwhen an entry is registered without atypefield.
``
``
142
`` +
- The
_ParamParsersslot is internal — vue-router's codegen populates it
``
``
143
`` +
- from files in
params/.
``
``
144
`+
`
``
145
`+
- @example
`
``
146
* ```ts
``
147
`+
- declare module 'vue-router' {
`
``
148
`+
- interface TypesConfig {
`
``
149
`+
- _ParamParsers: {
`
``
150
`+
- date: { type: Date }
`
``
151
`+
- }
`
``
152
`+
- }
`
``
153
`+
- }
`
``
154
* ```
``
155
`+
*/
`
``
156
`+
export type ParamParserTypeOf =
`
``
157
`+
Name extends keyof ParamParsers
`
``
158
`+
? ParamParsers[Name] extends { type: infer T }
`
``
159
`+
? T
`
``
160
`+
: unknown
`
``
161
`+
: unknown
`
``
162
+
``
163
`+
/**
`
``
164
`+
- Distributive variant of {@link DefinePageQueryParamOptions} used in the
`
``
165
`` +
params.queryrecord. Distributing overParamParserTypeproduces one
``
``
166
`` +
- variant per literal parser name so the
parserfield acts as a discriminant
``
``
167
`` +
- and
defaultis narrowed to that parser's resolved value type. Without
``
``
168
`` +
- this, the bare interface defaults
Parserto the fullParamParserType
``
``
169
`` +
- union and
defaultcollapses to the union of every parser's value type.
``
``
170
`+
`
``
171
`+
- @internal
`
``
172
`+
*/
`
``
173
`+
export type DefinePageQueryParamOptionsAny<
`
``
174
`+
P extends ParamParserType = ParamParserType,
`
``
175
`+
= P extends ParamParserType ? DefinePageQueryParamOptions
: never
`
114
176
``
115
177
`/**
`
116
178
` * Configures how to extract a route param from a specific query parameter.
`
``
179
`+
`
``
180
`+
- @typeParam Parser - name of the param parser used for this query parameter,
`
``
181
`` +
- used to type the {@link DefinePageQueryParamOptions.default |
default}
``
``
182
`+
- value via {@link ParamParserTypeOf}.
`
117
183
` */
`
118
``
`-
export interface DefinePageQueryParamOptions<T = unknown> {
`
``
184
`+
export interface DefinePageQueryParamOptions<
`
``
185
`+
Parser extends ParamParserType = ParamParserType,
`
``
186
`+
{
`
119
187
`/**
`
120
188
` * The type of the query parameter. Allowed values are native param parsers
`
121
189
` * and any parser in the {@link https://uvr.esm.is/TODO | params folder }. If
`
122
190
` * not provided, the value will kept as is.
`
123
191
` */
`
124
``
`-
parser?: ParamParserType
`
``
192
`+
parser?: Parser
`
125
193
``
126
194
`// TODO: allow customizing the name in the query string
`
127
195
`// queryKey?: string
`
`@@ -131,7 +199,7 @@ export interface DefinePageQueryParamOptions<T = unknown> {
`
131
199
` * (e.g. a invalid number is passed to the int param parser). If not provided
`
132
200
` * and the param is not required, the route will match with undefined.
`
133
201
` */
`
134
``
`-
default?: (() => T) | T
`
``
202
`+
default?: (() => ParamParserTypeOf) | ParamParserTypeOf
`
135
203
``
136
204
`/**
`
137
205
` * How to format the query parameter value.
`