Skip to content

Commit f7bfbea

Browse files
fix(client): fix TS errors that appear when users Go to Source in VSCode (#142)
1 parent 10fd687 commit f7bfbea

File tree

8 files changed

+54
-8
lines changed

8 files changed

+54
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ant
330330

331331
## Requirements
332332

333+
TypeScript >= 4.5 is supported.
334+
333335
The following runtimes are supported:
334336

335337
- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.

build

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ node scripts/fix-index-exports.cjs
3838
# index.d.mts the default import will work (even though both files have
3939
# the same export default statement)
4040
cp dist/index.d.ts dist/index.d.mts
41-
42-
SED=(sed -i)
43-
if [[ "$OSTYPE" == "darwin"* ]]; then SED=(sed -i ''); fi
41+
cp tsconfig.dist-src.json dist/src/tsconfig.json
4442

4543
# strip out lib="dom" and types="node" references; these are needed at build time,
4644
# but would pollute the user's TS environment
47-
REFERENCE_SUBS='s/^ *\/\/\/ *<reference *lib="dom".*//g;s/^ *\/\/\/ *<reference *types="node".*//g'
48-
find dist -type f -exec "${SED[@]}" "${REFERENCE_SUBS}" {} +
45+
find dist -type f -exec node scripts/remove-triple-slash-references.js {} +
46+
# strip out `unknown extends RequestInit ? never :` from dist/src/_shims;
47+
# these cause problems when viewing the .ts source files in go to definition
48+
find dist/src/_shims -type f -exec node scripts/replace-shim-guards.js {} +
4949

5050
npm exec prettier -- --loglevel=warn --write .
5151

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// strip out lib="dom" and types="node" references; these are needed at build time,
2+
// but would pollute the user's TS environment
3+
const fs = require('fs');
4+
for (const file of process.argv.slice(2)) {
5+
const before = fs.readFileSync(file, 'utf8');
6+
const after = before.replace(/^ *\/\/\/ *<reference +(lib="dom"|types="node").*?\n/gm, '');
7+
if (after !== before) {
8+
fs.writeFileSync(file, after, 'utf8');
9+
console.error('wrote', file);
10+
}
11+
}

scripts/replace-shim-guards.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// strip out `unknown extends RequestInit ? never :` from dist/src/_shims;
2+
// these cause problems when viewing the .ts source files in go to definition
3+
const fs = require('fs');
4+
for (const file of process.argv.slice(2)) {
5+
const before = fs.readFileSync(file, 'utf8');
6+
const after = before.replace(
7+
new RegExp('unknown extends (typeof )?\\S+ \\? \\S+ :\\s*'.replace(/\s+/, '\\s+'), 'gm'),
8+
'',
9+
);
10+
if (after !== before) {
11+
fs.writeFileSync(file, after, 'utf8');
12+
console.error('wrote', file);
13+
}
14+
}

src/_shims/fetch-deno.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const _fetch = fetch;
2+
type _fetch = typeof fetch;
23
const _Request = Request;
4+
type _Request = Request;
35
type _RequestInfo = RequestInfo;
46
type _RequestInit = RequestInit;
57
const _Response = Response;
8+
type _Response = Response;
69
type _ResponseInit = ResponseInit;
710
type _BodyInit = BodyInit;
811
const _Headers = Headers;
12+
type _Headers = Headers;
913
type _HeadersInit = HeadersInit;
1014

1115
export const isPolyfilled = false;

src/_shims/form-data-deno.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ type _BlobPropertyBag = BlobPropertyBag;
22
type _FilePropertyBag = FilePropertyBag;
33

44
const _FormData = FormData;
5+
type _FormData = FormData;
56
const _File = File;
7+
type _File = File;
68
const _Blob = Blob;
9+
type _Blob = Blob;
710

811
export const isPolyfilled = false;
912

src/core.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
5252
// TODO handle blob, arraybuffer, other content types, etc.
5353
const text = await response.text();
5454
debug('response', response.status, response.url, response.headers, text);
55-
return text as T;
55+
return text as any as T;
5656
}
5757

5858
/**
@@ -313,7 +313,8 @@ export abstract class APIClient {
313313
protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string> {
314314
return (
315315
!headers ? {}
316-
: Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header]))
316+
: Symbol.iterator in headers ?
317+
Object.fromEntries(Array.from(headers as Iterable<string[]>).map((header) => [...header]))
317318
: { ...headers }
318319
);
319320
}
@@ -397,7 +398,7 @@ export abstract class APIClient {
397398
return new PagePromise<PageClass, Item>(this, request, Page);
398399
}
399400

400-
buildURL<Req>(path: string, query: Req | undefined): string {
401+
buildURL<Req extends Record<string, unknown>>(path: string, query: Req | null | undefined): string {
401402
const url =
402403
isAbsoluteURL(path) ?
403404
new URL(path)

tsconfig.dist-src.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
// this config is included in the published src directory to prevent TS errors
3+
// from appearing when users go to source, and VSCode opens the source .ts file
4+
// via declaration maps
5+
"include": ["index.ts"],
6+
"compilerOptions": {
7+
"target": "es2015",
8+
"lib": ["DOM"],
9+
"moduleResolution": "node"
10+
}
11+
}

0 commit comments

Comments
 (0)