node-modules / urllib

Request HTTP(s) URLs in a complex world.
MIT License
725 stars 115 forks source link

Return type of `request` function is `any` #464

Closed tremby closed 11 months ago

tremby commented 11 months ago

I'm using node 18.16.1 and typescript 5.2.2.

My tsconfig.json:

{
    "extends": ["@tsconfig/strictest/tsconfig", "@tsconfig/node18/tsconfig"],
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "outDir": "dist"
    },
    "include": ["src/**/*"]
}

A file src/test.mts:

import { request } from "urllib";
const test = await request("test");

The variable test has type any.

If I switch off skipLibCheck in typescript, I see a number of errors in urllib:

node_modules/urllib/src/esm/index.d.ts:1:44 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './Request.js'?

1 import { RequestOptions, RequestURL } from './Request';
                                             ~~~~~~~~~~~

node_modules/urllib/src/esm/index.d.ts:2:101 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16'
or 'nodenext'. Did you mean './Response.js'?

2 export declare function request<T = any>(url: RequestURL, options?: RequestOptions): Promise<import("./Response").HttpClientResponse<T>>;
                                                                                                      ~~~~~~~~~~~~

node_modules/urllib/src/esm/index.d.ts:3:98 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './Response.js'?

3 export declare function curl<T = any>(url: RequestURL, options?: RequestOptions): Promise<import("./Response").HttpClientResponse<T>>;
                                                                                                   ~~~~~~~~~~~~

node_modules/urllib/src/esm/index.d.ts:5:144 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16'
or 'nodenext'. Did you mean './HttpClient.js'?

5 export { HttpClient, HttpClient as HttpClient2, HEADER_USER_AGENT as USER_AGENT, RequestDiagnosticsMessage, ResponseDiagnosticsMessage, } from './HttpClient';
                                                                                                                                                 ~~~~~~~~~~~~~~

node_modules/urllib/src/esm/index.d.ts:6:133 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16'
or 'nodenext'. Did you mean './Request.js'?

6 export { RequestOptions, RequestOptions as RequestOptions2, RequestURL, HttpMethod, FixJSONCtlCharsHandler, FixJSONCtlChars, } from './Request';
                                                                                                                                      ~~~~~~~~~~~

node_modules/urllib/src/esm/index.d.ts:7:77 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './Response.js'?

7 export { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response';
                                                                              ~~~~~~~~~~~~

I'm not sure if they're related.

Am I doing something wrong? Something wrong with my TS configuration? Am I using an unsupported target?

fengmk2 commented 11 months ago

I'll take a look at it.

fengmk2 commented 11 months ago

Can you show me your package.json settings?

fengmk2 commented 11 months ago

image

I can't reproduce the tsc build error

tremby commented 11 months ago

Unless I switch off skipLibCheck in the TS config, it's not a typescript error I'm receiving at build time.

Here: https://github.com/tremby/urllib-type-issue

The issue I'm seeing is the type that the request method returns. It's coming back as any, which means I don't get all the nice type inference and safety and suggestions in my editor I should get. See screenshots:

Vim (with ALE, which is running tsserver, and which correctly picks up the project's configuration) reports that responseObj has type any:

2023-09-09_16-20

Vim reports that x on the highlighted line has type true, which is a roundabout way of confirming that responseObj has type any:

2023-09-09_16-14

tremby commented 11 months ago

Thanks. Types are appearing correctly now.