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

`+

`

``

109

`+

`

``

110

`+

`

``

111

`+

`

``

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

`+

`

``

122

`+

`

``

123

`` +

``

``

124

`+

`

``

125

`` +

``

``

126

`+

`

``

127

`+

`

``

128

`+

*/

`

``

129

`+

export type ParamParsers = ParamParsers_Native &

`

``

130

`+

(TypesConfig extends { _ParamParsers: infer P } ? P : {})

`

``

131

+

``

132

`+

/**

`

``

133

`+

`

``

134

`+

*/

`

``

135

`+

export type ParamParserType = keyof ParamParsers

`

``

136

+

``

137

`+

/**

`

``

138

`+

`

``

139

`` +

``

``

140

`+

`

``

141

`` +

``

``

142

`` +

``

``

143

`` +

``

``

144

`+

`

``

145

`+

`

``

146


 * ```ts

``

147

`+

`

``

148

`+

`

``

149

`+

`

``

150

`+

`

``

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

`+

`

``

165

`` +

``

``

166

`` +

``

``

167

`` +

``

``

168

`` +

``

``

169

`` +

``

``

170

`+

`

``

171

`+

`

``

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

`+

`

``

181

`` +

``

``

182

`+

`

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.

`