@@ -8,11 +8,9 @@ Query parameters are represented as the type
88` Record<string, string | string[] | undefined> ` , so using a codec that doesn't decode
99from a ` string | string[] | undefined ` will produce a type error.
1010
11- <CH.Spotlight>
12-
1311Consider this ` httpRoute ` that compiles successfully.
1412
15- ``` typescript spec.ts
13+ ``` typescript
1614import * as t from ' io-ts' ;
1715import { httpRoute , httpRequest } from ' @api-ts/io-ts-http' ;
1816
@@ -30,9 +28,10 @@ const GetHello = httpRoute({
3028});
3129```
3230
33- ---
31+ If you add an expected ` number ` value to the ` httpRoute ` 's query parameters, you'll see
32+ the following compilation error:
3433
35- ``` typescript spec.ts focus=14:16
34+ ``` typescript
3635import * as t from ' io-ts' ;
3736import { httpRoute , httpRequest } from ' @api-ts/io-ts-http' ;
3837
@@ -72,8 +71,6 @@ number.
7271We need a codec that decodes a ` string ` into a ` number ` and converts the
7372string-representation of a number into the ` number ` type.
7473
75- ---
76-
7774This is a fairly common requirement, so this codec already exists: [ io-ts-types] offers
7875the [ NumberFromString] codec that decodes a ` string ` value into a ` number ` . Use
7976` NumberFromString ` to fix your compilation error.
@@ -82,7 +79,7 @@ the [NumberFromString] codec that decodes a `string` value into a `number`. Use
8279[ numberfromstring] :
8380 https://gcanti.github.io/io-ts-types/modules/NumberFromString.ts.html
8481
85- ``` typescript spec.ts focus=2,15:17
82+ ``` typescript
8683import * as t from ' io-ts' ;
8784import { NumberFromString } from ' io-ts-types' ;
8885import { httpRoute , httpRequest } from ' @api-ts/io-ts-http' ;
@@ -104,7 +101,5 @@ const GetHello = httpRoute({
104101});
105102```
106103
107- </CH.Spotlight>
108-
109104In general, the solution to decoding a query parameter into a non-string type is to use
110105a codec that decodes and encodes from a ` string ` into your desired type.
0 commit comments