Skip to content
Myr

Document that `null` is intentionally rejected by the `searchParams` type

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The runtime sends `null` values as the string `'null'`, but the type stays strict so accidental nulls are caught at compile time. Add a type test for the rejection and runtime tests for the string conversion through direct calls, `.extend()`, and the tuple form.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent e087b0c · Browse files · 3 files+12 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+12 added−0 removed

  • tests:+5 added−0 removed
  • other:+7 added−0 removed

3 files+12 added−0 removed

modified:source/types/options.ts+1 added
3 unmodified lines
45// eslint-disable-next-line unicorn/prevent-abbreviations6export type SearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams | undefined;78// `null` is intentionally not allowed in the object form even though the runtime sends it as the string `'null'` (like `URLSearchParams` does), so accidental nulls are caught by the type checker.9// eslint-disable-next-line unicorn/prevent-abbreviations10export type SearchParamsOption = SearchParamsInit | Record<string, string | number | boolean | undefined> | Array<Array<string | number | boolean>>;1112export type RequestHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'query';
482 unmodified lines
modified:test/main.ts+5 added
1252 unmodified lines
1253 opossums: 'false',1254 birds: undefined,1255 };12561257 // `null` is rejected by the type on purpose, but the runtime still sends it as the string `'null'`.1258 const objectWithNull = {1259 cats: 'meow',1260 dogs: null as any,1261 opossums: 'false',
3 unmodified lines
1265 t.is(await ky(server.url, {searchParams: objectWithUndefined}).text(), '?cats=meow&opossums=false');12661267 // Null values should be preserved as string "null"1268 t.is(await ky(server.url, {searchParams: objectWithNull}).text(), '?cats=meow&dogs=null&opossums=false');12691270 // The same applies through `.extend()` merging and to the tuple form1271 t.is(await ky.extend({searchParams: {a: null as any}})(server.url, {searchParams: {b: null as any}}).text(), '?a=null&b=null');1272 t.is(await ky(server.url, {searchParams: [['a', null as any]]}).text(), '?a=null');1273});12741275test('merges searchParams with input URL', async t => {1276 const server = await createHttpTestServer(t);
2208 unmodified lines
modified:test-d/normalized-options.ts+6 added