Skip to content

Commit

Permalink
Upgrade to TypeScript 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 14, 2020
1 parent 761b8e0 commit 2e349d1
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 73 deletions.
2 changes: 1 addition & 1 deletion benchmark/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {AddressInfo} from 'net';
import https = require('https');
// @ts-ignore No types
// @ts-expect-error No types
import createCert = require('create-cert');

(async () => {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@sindresorhus/tsconfig": "^0.7.0",
"@types/benchmark": "^1.0.31",
"@types/benchmark": "^1.0.32",
"@types/express": "^4.17.6",
"@types/lolex": "^5.1.0",
"@types/node": "^13.13.4",
"@types/node": "^14.0.1",
"@types/node-fetch": "^2.5.5",
"@types/request": "^2.48.4",
"@types/sinon": "^9.0.0",
"@types/tough-cookie": "^4.0.0",
"ava": "^3.6.0",
"ava": "^3.8.2",
"axios": "^0.19.2",
"benchmark": "^2.1.4",
"coveralls": "^3.0.4",
Expand All @@ -86,7 +86,7 @@
"tempy": "^0.5.0",
"to-readable-stream": "^2.1.0",
"tough-cookie": "^4.0.0",
"typescript": "3.8.3",
"typescript": "3.9.2",
"xo": "^0.30.0"
},
"types": "dist/source",
Expand Down
3 changes: 1 addition & 2 deletions source/as-promise/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ if (!knownHookEvents.includes('beforeRetry' as any)) {

export const knownBodyTypes = ['json', 'buffer', 'text'];

// @ts-ignore The error is: Not all code paths return a value.
export const parseBody = (response: Response, responseType: ResponseType, encoding?: string): unknown => {
export const parseBody = (response: Response, responseType: ResponseType, encoding?: BufferEncoding): unknown => {
const {rawBody} = response;

try {
Expand Down
2 changes: 1 addition & 1 deletion source/as-promise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function asPromise<T>(options: NormalizedOptions): CancelableRequ

try {
for (const [index, hook] of options.hooks.afterResponse.entries()) {
// @ts-ignore TS doesn't notice that CancelableRequest is a Promise
// @ts-expect-error TS doesn't notice that CancelableRequest is a Promise
// eslint-disable-next-line no-await-in-loop
response = await hook(response, async (updatedOptions): CancelableRequest<Response> => {
const typedOptions = PromisableRequest.normalizeArguments(undefined, {
Expand Down
16 changes: 8 additions & 8 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import https = require('https');
import timer, {ClientRequestWithTimings, Timings, IncomingMessageWithTimings} from '@szmarczak/http-timer';
import CacheableLookup from 'cacheable-lookup';
import CacheableRequest = require('cacheable-request');
// @ts-ignore Missing types
// @ts-expect-error Missing types
import http2wrapper = require('http2-wrapper');
import lowercaseKeys = require('lowercase-keys');
import ResponseLike = require('responselike');
Expand Down Expand Up @@ -297,7 +297,7 @@ const setNonEnumerableProperties = (sources: Array<Options | Defaults | undefine
writable: true,
configurable: true,
enumerable: false,
// @ts-ignore TS doesn't see the check above
// @ts-expect-error TS doesn't see the check above
value: source[name]
};
}
Expand Down Expand Up @@ -533,7 +533,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
if (kIsNormalizedAlready in nonNormalizedOptions) {
this.options = nonNormalizedOptions as NormalizedOptions;
} else {
// @ts-ignore Common TypeScript bug saying that `this.constructor` is not accessible
// @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
this.options = this.constructor.normalizeArguments(url, nonNormalizedOptions, defaults);
}

Expand Down Expand Up @@ -1207,7 +1207,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
this._unlockWrite();

if (!is.undefined(body)) {
this._writeRequest(body, null as unknown as string, () => {});
this._writeRequest(body, null as unknown as BufferEncoding, () => {});
currentRequest.end();

this._lockWrite();
Expand Down Expand Up @@ -1280,7 +1280,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
const result = await hook(options);

if (!is.undefined(result)) {
// @ts-ignore Skip the type mismatch to support abstract responses
// @ts-expect-error Skip the type mismatch to support abstract responses
options.request = () => result;
break;
}
Expand Down Expand Up @@ -1426,7 +1426,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}
}

_write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void {
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
const write = (): void => {
this._writeRequest(chunk, encoding, callback);
};
Expand All @@ -1438,9 +1438,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}
}

_writeRequest(chunk: any, encoding: string, callback: (error?: Error | null) => void): void {
_writeRequest(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
this._progressCallbacks.push((): void => {
this[kUploadedSize] += Buffer.byteLength(chunk, encoding as BufferEncoding);
this[kUploadedSize] += Buffer.byteLength(chunk, encoding);

const progress = this.uploadProgress;

Expand Down
1 change: 1 addition & 0 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const create = (defaults: InstanceDefaults): Got => {

// Pagination
const paginateEach = (async function * <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) {
// @ts-expect-error FIXME
let normalizedOptions = normalizeArguments(url, options, defaults.options);
normalizedOptions.resolveBodyOnly = false;

Expand Down
4 changes: 2 additions & 2 deletions test/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const prepareServer = (server: ExtendedTestServer): void => {

const createAgentSpy = <T extends HttpsAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
const agent: T = new AgentClass({keepAlive: true});
// @ts-ignore This IS correct
// @ts-expect-error This IS correct
const spy = sinon.spy(agent, 'addRequest');
return {agent, spy};
};
Expand Down Expand Up @@ -136,7 +136,7 @@ test('socket connect listener cleaned up after request', withServer, async (t, s
});
}

// @ts-ignore
// @ts-expect-error
for (const value of Object.values(agent.freeSockets) as [Socket[]]) {
for (const sock of value) {
t.is(sock.listenerCount('connect'), 0);
Expand Down
28 changes: 12 additions & 16 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('`url` should be utf-8 encoded', async t => {
});

test('throws if no arguments provided', async t => {
// @ts-ignore Error tests
// @ts-expect-error Error tests
await t.throwsAsync(got(), {
message: 'Missing `url` property'
});
Expand Down Expand Up @@ -93,7 +93,7 @@ test.failing('throws an error when legacy URL is passed', withServer, async (t,
server.get('/test', echoUrl);

await t.throwsAsync(
// @ts-ignore Error tests
// @ts-expect-error Error tests
got(parse(`${server.url}/test`), {prefixUrl: ''}),
{message: 'The legacy `url.Url` has been deprecated. Use `URL` instead.'}
);
Expand Down Expand Up @@ -177,7 +177,6 @@ test('ignores empty searchParams object', withServer, async (t, server, got) =>
});

test('throws when passing body with a non payload method', async t => {
// @ts-ignore Error tests
await t.throwsAsync(got('https://example.com', {body: 'asdf'}), {
message: 'The `GET` method cannot be used with a body'
});
Expand Down Expand Up @@ -220,7 +219,7 @@ test('can omit `url` option if using `prefixUrl`', withServer, async (t, server,

test('throws TypeError when `options.hooks` is not an object', async t => {
await t.throwsAsync(
// @ts-ignore Error tests
// @ts-expect-error Error tests
got('https://example.com', {hooks: 'not object'}),
{
message: 'Expected value which is `predicate returns truthy for any value`, received value of type `Array`.'
Expand All @@ -230,7 +229,7 @@ test('throws TypeError when `options.hooks` is not an object', async t => {

test('throws TypeError when known `options.hooks` value is not an array', async t => {
await t.throwsAsync(
// @ts-ignore Error tests
// @ts-expect-error Error tests
got('https://example.com', {hooks: {beforeRequest: {}}}),
{
message: 'Parameter `beforeRequest` must be an Array, got Object'
Expand All @@ -239,9 +238,8 @@ test('throws TypeError when known `options.hooks` value is not an array', async
});

test('throws TypeError when known `options.hooks` array item is not a function', async t => {
// @ts-ignore Error tests
await t.throwsAsync(
// @ts-ignore Error tests
// @ts-expect-error Error tests
got('https://example.com', {hooks: {beforeRequest: [{}]}}),
{
message: 'hook is not a function'
Expand All @@ -252,7 +250,7 @@ test('throws TypeError when known `options.hooks` array item is not a function',
test('allows extra keys in `options.hooks`', withServer, async (t, server, got) => {
server.get('/test', echoUrl);

// @ts-ignore We do not allow extra keys in hooks but this won't throw
// @ts-expect-error We do not allow extra keys in hooks but this won't throw
await t.notThrowsAsync(got('test', {hooks: {extra: []}}));
});

Expand Down Expand Up @@ -313,10 +311,9 @@ test('throws if cannot change `prefixUrl`', async t => {
});

test('throws if the `searchParams` value is invalid', async t => {
// @ts-ignore Error tests
await t.throwsAsync(got('https://example.com', {
searchParams: {
// @ts-ignore Error tests
// @ts-expect-error Error tests
foo: []
}
}), {
Expand Down Expand Up @@ -377,9 +374,8 @@ test('`context` option is accessible when extending instances', t => {
});

test('throws if `options.encoding` is `null`', async t => {
// @ts-ignore Error tests
await t.throwsAsync(got('https://example.com', {
// @ts-ignore For testing purposes
// @ts-expect-error For testing purposes
encoding: null
}), {message: 'To get a Buffer, set `options.responseType` to `buffer` instead'});
});
Expand All @@ -392,7 +388,7 @@ test('`url` option and input argument are mutually exclusive', async t => {

test('throws a helpful error when passing `followRedirects`', async t => {
await t.throwsAsync(got('https://example.com', {
// @ts-ignore For testing purposes
// @ts-expect-error For testing purposes
followRedirects: true
}), {message: 'The `followRedirects` option does not exist. Use `followRedirect` instead.'});
});
Expand All @@ -410,7 +406,7 @@ test('merges `searchParams` instances', t => {

test('throws a helpful error when passing `auth`', async t => {
await t.throwsAsync(got('https://example.com', {
// @ts-ignore For testing purposes
// @ts-expect-error For testing purposes
auth: 'username:password'
}), {
message: 'Parameter `auth` is deprecated. Use `username` / `password` instead.'
Expand All @@ -425,15 +421,15 @@ test('throws on leading slashes', async t => {

test('throws on invalid `dnsCache` option', async t => {
await t.throwsAsync(got('https://example.com', {
// @ts-ignore Error tests
// @ts-expect-error Error tests
dnsCache: 123
}), {message: 'Parameter `dnsCache` must be a CacheableLookup instance or a boolean, got number'});
});

test('throws on invalid `agent` option', async t => {
await t.throwsAsync(got('https://example.com', {
agent: {
// @ts-ignore Error tests
// @ts-expect-error Error tests
asdf: 123
}
}), {message: 'Expected the `options.agent` properties to be `http`, `https` or `http2`, got `asdf`'});
Expand Down
4 changes: 2 additions & 2 deletions test/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ test('cache error throws `got.CacheError`', withServer, async (t, server, got) =

const cache = {};

// @ts-ignore Error tests
// @ts-expect-error Error tests
await t.throwsAsync(got({cache}), {instanceOf: got.CacheError});
});

Expand Down Expand Up @@ -151,7 +151,7 @@ test('DNS cache works', withServer, async (t, _server, got) => {

await t.notThrowsAsync(instance('https://example.com'));

// @ts-ignore
// @ts-expect-error
t.is(instance.defaults.options.dnsCache!._cache.size, 1);
});

Expand Down
4 changes: 2 additions & 2 deletions test/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ test('accepts custom `cookieJar` object', withServer, async (t, server, got) =>
test('throws on invalid `options.cookieJar.setCookie`', async t => {
await t.throwsAsync(got('https://example.com', {
cookieJar: {
// @ts-ignore Error tests
// @ts-expect-error Error tests
setCookie: 123
}
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
Expand All @@ -188,7 +188,7 @@ test('throws on invalid `options.cookieJar.getCookieString`', async t => {
await t.throwsAsync(got('https://example.com', {
cookieJar: {
setCookie: async () => {},
// @ts-ignore Error tests
// @ts-expect-error Error tests
getCookieString: 123
}
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
Expand Down
10 changes: 5 additions & 5 deletions test/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ test('defaults are cloned on instance creation', t => {
delete options.hooks.beforeRequest[0];
});

// @ts-ignore This IS correct
// @ts-expect-error This IS correct
t.not(options.foo, instance.defaults.options.foo);
t.not(options.hooks.beforeRequest, instance.defaults.options.hooks.beforeRequest);
});
Expand All @@ -197,7 +197,7 @@ test('ability to pass a custom request method', withServer, async (t, server, go
((res: IncomingMessage) => void)?
]) => {
isCalled = true;
// @ts-ignore Overload error
// @ts-expect-error Overload error
return httpRequest(...args);
};

Expand All @@ -221,7 +221,7 @@ test('does not include the `request` option in normalized `http` options', withS

t.false(Reflect.has(args[0] as RequestOptions, 'request'));

// @ts-ignore Overload error
// @ts-expect-error Overload error
return httpRequest(...args);
};

Expand Down Expand Up @@ -285,7 +285,7 @@ test('async handlers', withServer, async (t, server, got) => {
handlers: [
async (options, next) => {
const result = await next(options);
// @ts-ignore Manual tests
// @ts-expect-error Manual tests
result.modified = true;

return result;
Expand All @@ -295,7 +295,7 @@ test('async handlers', withServer, async (t, server, got) => {

const promise = instance('');
t.true(is.function_(promise.cancel));
// @ts-ignore Manual tests
// @ts-expect-error Manual tests
t.true((await promise).modified);
});

Expand Down
7 changes: 3 additions & 4 deletions test/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('catches dns errors', async t => {
});

test('`options.body` form error message', async t => {
// @ts-ignore Error tests
// @ts-expect-error Error tests
await t.throwsAsync(got.post('https://example.com', {body: Buffer.from('test'), form: ''}), {
message: 'The `body`, `json` and `form` options are mutually exclusive'
});
Expand Down Expand Up @@ -134,9 +134,8 @@ test('`http.request` error', async t => {
test('`http.request` pipe error', async t => {
const message = 'snap!';

// @ts-ignore Error tests
await t.throwsAsync(got('https://example.com', {
// @ts-ignore Error tests
// @ts-expect-error Error tests
request: () => {
const proxy = new stream.PassThrough();

Expand Down Expand Up @@ -180,7 +179,7 @@ test('`http.request` error through CacheableRequest', async t => {

test('errors are thrown directly when options.isStream is true', t => {
t.throws(() => {
// @ts-ignore Error tests
// @ts-expect-error Error tests
got('https://example.com', {isStream: true, hooks: false});
}, {
message: 'Expected value which is `predicate returns truthy for any value`, received value of type `Array`.'
Expand Down
4 changes: 2 additions & 2 deletions test/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test('does not set `accept-encoding` header when `options.decompress` is false',
const headers = await got({
decompress: false
}).json();
// @ts-ignore Error tests
// @ts-expect-error Error tests
t.false(Reflect.has(headers, 'accept-encoding'));
});

Expand Down Expand Up @@ -201,7 +201,7 @@ test('throws on null value headers', async t => {
await t.throwsAsync(got({
url: 'https://example.com',
headers: {
// @ts-ignore Testing purposes
// @ts-expect-error Testing purposes
'user-agent': null
}
}), {
Expand Down

0 comments on commit 2e349d1

Please sign in to comment.