Skip to content

Commit e27e0f6

Browse files
authored
types: add Autocomplete utility type (#3479)
1 parent 5af9963 commit e27e0f6

File tree

3 files changed

+66
-54
lines changed

3 files changed

+66
-54
lines changed

types/dispatcher.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IncomingHttpHeaders } from './header'
66
import BodyReadable from './readable'
77
import { FormData } from './formdata'
88
import Errors from './errors'
9+
import { Autocomplete } from './utility'
910

1011
type AbortSignal = unknown;
1112

@@ -234,7 +235,7 @@ declare namespace Dispatcher {
234235
onBodySent?(chunkSize: number, totalBytesSent: number): void;
235236
}
236237
export type PipelineHandler<TOpaque = null> = (data: PipelineHandlerData<TOpaque>) => Readable;
237-
export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH' | (string & Record<never, never>);
238+
export type HttpMethod = Autocomplete<'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'>;
238239

239240
/**
240241
* @link https://fetch.spec.whatwg.org/#body-mixin

types/header.d.ts

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { Autocomplete } from "./utility";
2+
13
/**
24
* The header type declaration of `undici`.
35
*/
46
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;
57

6-
type HeaderNames =
8+
type HeaderNames = Autocomplete<
79
| 'Accept'
810
| 'Accept-CH'
911
| 'Accept-Charset'
@@ -93,60 +95,62 @@ type HeaderNames =
9395
| 'WWW-Authenticate'
9496
| 'X-Content-Type-Options'
9597
| 'X-Frame-Options'
96-
| (string & {})
98+
>
99+
100+
type IANARegisteredMimeType = Autocomplete<
101+
| 'audio/aac'
102+
| 'video/x-msvideo'
103+
| 'image/avif'
104+
| 'video/av1'
105+
| 'application/octet-stream'
106+
| 'image/bmp'
107+
| 'text/css'
108+
| 'text/csv'
109+
| 'application/vnd.ms-fontobject'
110+
| 'application/epub+zip'
111+
| 'image/gif'
112+
| 'application/gzip'
113+
| 'text/html'
114+
| 'image/x-icon'
115+
| 'text/calendar'
116+
| 'image/jpeg'
117+
| 'text/javascript'
118+
| 'application/json'
119+
| 'application/ld+json'
120+
| 'audio/x-midi'
121+
| 'audio/mpeg'
122+
| 'video/mp4'
123+
| 'video/mpeg'
124+
| 'audio/ogg'
125+
| 'video/ogg'
126+
| 'application/ogg'
127+
| 'audio/opus'
128+
| 'font/otf'
129+
| 'application/pdf'
130+
| 'image/png'
131+
| 'application/rtf'
132+
| 'image/svg+xml'
133+
| 'image/tiff'
134+
| 'video/mp2t'
135+
| 'font/ttf'
136+
| 'text/plain'
137+
| 'application/wasm'
138+
| 'video/webm'
139+
| 'audio/webm'
140+
| 'image/webp'
141+
| 'font/woff'
142+
| 'font/woff2'
143+
| 'application/xhtml+xml'
144+
| 'application/xml'
145+
| 'application/zip'
146+
| 'video/3gpp'
147+
| 'video/3gpp2'
148+
| 'model/gltf+json'
149+
| 'model/gltf-binary'
150+
>
97151

98152
type KnownHeaderValues = {
99-
'content-type':
100-
| 'audio/aac'
101-
| 'video/x-msvideo'
102-
| 'image/avif'
103-
| 'video/av1'
104-
| 'application/octet-stream'
105-
| 'image/bmp'
106-
| 'text/css'
107-
| 'text/csv'
108-
| 'application/vnd.ms-fontobject'
109-
| 'application/epub+zip'
110-
| 'image/gif'
111-
| 'application/gzip'
112-
| 'text/html'
113-
| 'image/x-icon'
114-
| 'text/calendar'
115-
| 'image/jpeg'
116-
| 'text/javascript'
117-
| 'application/json'
118-
| 'application/ld+json'
119-
| 'audio/x-midi'
120-
| 'audio/mpeg'
121-
| 'video/mp4'
122-
| 'video/mpeg'
123-
| 'audio/ogg'
124-
| 'video/ogg'
125-
| 'application/ogg'
126-
| 'audio/opus'
127-
| 'font/otf'
128-
| 'application/pdf'
129-
| 'image/png'
130-
| 'application/rtf'
131-
| 'image/svg+xml'
132-
| 'image/tiff'
133-
| 'video/mp2t'
134-
| 'font/ttf'
135-
| 'text/plain'
136-
| 'application/wasm'
137-
| 'video/webm'
138-
| 'audio/webm'
139-
| 'image/webp'
140-
| 'font/woff'
141-
| 'font/woff2'
142-
| 'application/xhtml+xml'
143-
| 'application/xml'
144-
| 'application/zip'
145-
| 'video/3gpp'
146-
| 'video/3gpp2'
147-
| 'model/gltf+json'
148-
| 'model/gltf-binary'
149-
| (string & {})
153+
'content-type': IANARegisteredMimeType
150154
}
151155

152156
export type HeaderRecord = {

types/utility.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type AutocompletePrimitiveBaseType<T> =
2+
T extends string ? string :
3+
T extends number ? number :
4+
T extends boolean ? boolean :
5+
never;
6+
7+
export type Autocomplete<T> = T | (AutocompletePrimitiveBaseType<T> & Record<never, never>);

0 commit comments

Comments
 (0)