Got: TS2416 when using @types/node < 14.0.19

Created on 8 Jul 2020  Â·  12Comments  Â·  Source: sindresorhus/got

Describe the bug

  • Node.js version: v12.18.2
  • OS & version: node:12-alpine

Attempting to execute tsc on project using got == 11.5.0 results in TS2416 from got source code. Using @types/node v14.0.19 solves the issue but I should not be forced to use types for newer Node version that I use (that might result in runtime errors when using features not present in Node v12 but visible in types for v14)

"engines": {
  "node": ">=12.10.0"
},
"dependencies": {
  "got": "11.5.0"
},
"devDependencies": {
  "@types/node": "12.12.48"
}

Actual behavior

+ yarn --frozen-lockfile --non-interactive
yarn install v1.22.4
[1/5] Validating package.json...
[2/5] Resolving packages...
[3/5] Fetching packages...
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[4/5] Linking dependencies...
[5/5] Building fresh packages...
Done in 41.66s.
+ yarn lint
yarn run v1.22.4
$ run-p --aggregate-output --continue-on-error lint:*
$ prettier --check 'src/**/*.ts'
Checking formatting...
All matched files use Prettier code style!
$ eslint --ext .ts --format unix src/
Done in 9.12s.
+ yarn build
yarn run v1.22.4
$ rm -rf dist/ && tsc
node_modules/got/dist/source/core/index.d.ts(299,5): error TS2416: Property '_write' in type 'Request' is not assignable to the same property in base type 'Duplex'.
  Type '(chunk: any, encoding: "utf8" | "ascii" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined, callback: (error?: ErrnoException | null | undefined) => void) => void' is not assignable to type '(chunk: any, encoding: string, callback: (error?: Error | null | undefined) => void) => void'.
    Types of parameters 'encoding' and 'encoding' are incompatible.
      Type 'string' is not assignable to type '"utf8" | "ascii" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined'.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Expected behavior

No build errors

Code to reproduce

https://github.com/proton-ab/kana/pull/1

Checklist

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.
external types ✭ help wanted ✭

Most helpful comment

I think the // ts-ignore is stripped from the distributed files. This is with 11.5.0:

node_modules/got/dist/source/core/index.d.ts:299:5 - error TS2416: Property '_write' in type 'Request' is not assignable to the same property in base type 'Duplex'.
  Type '(chunk: any, encoding: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined, callback: (error?: ErrnoException | null | undefined) => void) => void' is not assignable to type '(chunk: any, encoding: string, callback: (error?: Error | null | undefined) => void) => void'.
    Types of parameters 'encoding' and 'encoding' are incompatible.
      Type 'string' is not assignable to type '"ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined'.

299     _write(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
        ~~~~~~

Note line 299 here, which does not correspond to the source code here on GitHub.

All 12 comments

We're experiencing the same issue. CI broke when moving from 11.4.0 to 11.5.0 (got is accidental moving dependency).

/cc @sindresorhus Looks like a TypeScript issue. encoding cannot be a string because not all strings are encodings. They fixed it in the Node.js 14 types but didn't do in the Node.js 13 types.

Alternatively we could workaround this. Just a simple // @ts-ignore should fix that error.

Yeah, we can just ts-ignore with a todo comment to remove the ignore when targeting Node.js 14.

Hello there,

Same issue here (using node 12 types.)

Eagerly waiting for next release.

@szmarczak @sindresorhus maybe it is silly but... since package.json is set with "node": ">=10.19.0" wouldn't it be sensible to use "@types/node": "^10.x.x"?

I think the // ts-ignore is stripped from the distributed files. This is with 11.5.0:

node_modules/got/dist/source/core/index.d.ts:299:5 - error TS2416: Property '_write' in type 'Request' is not assignable to the same property in base type 'Duplex'.
  Type '(chunk: any, encoding: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined, callback: (error?: ErrnoException | null | undefined) => void) => void' is not assignable to type '(chunk: any, encoding: string, callback: (error?: Error | null | undefined) => void) => void'.
    Types of parameters 'encoding' and 'encoding' are incompatible.
      Type 'string' is not assignable to type '"ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined'.

299     _write(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
        ~~~~~~

Note line 299 here, which does not correspond to the source code here on GitHub.

Unfortunately we cannot place // @ts-nocheck at the beginning because it would error that there are unused @ts-expect-error directives. ~I have no idea how to fix this.~

/cc @sindresorhus

I think the fix is

    _write(chunk: any, encoding: string | undefined, callback: (error?: Error | null) => void): void {
        const write = (): void => {
            this._writeRequest(chunk, encoding as BufferEncoding, callback);
        };

any time frame when this gets released?

I'll do a release later today

Released 11.5.1.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

darksabrefr picture darksabrefr  Â·  3Comments

alanzhaonys picture alanzhaonys  Â·  4Comments

carvallegro picture carvallegro  Â·  4Comments

f-mer picture f-mer  Â·  4Comments

jamestalmage picture jamestalmage  Â·  3Comments