Eyal-Shalev/docopt.js

TypeScript 3.9 error "TS2322 [ERROR]: Type 'unknown[]' is not assignable to type 'T[]'"

shah opened this issue · 9 comments

shah commented

I've been using docopt.js in Deno for some time with TypeScript 3.8.3 with no problem. I love docopt and really enjoy how easy it use to use.

Recently Deno upgraded to TS 3.9 and I'm seeing the following error:

error: TS2322 [ERROR]: Type 'unknown[]' is not assignable to type 'T[]'.
  Type 'unknown' is not assignable to type 'T'.
    'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
  return Array.prototype.flat?.apply(arr, [depth]) || (
  
    at https://raw.githubusercontent.com/Eyal-Shalev/docopt.js/master/src/docopt.ts:676:3

Any thoughts for a solution/fix?

I'm glad to hear you are enjoying docopt.js, and will try to fix it!

@shah Could you provide a minimal code example that demonstrates this issue.
All of my normal tests pass with deno 1.0.0

shah commented

Thanks for checking @Eyal-Shalev.

I'm able to reproduce it with deno eval:

$ deno --version
deno 1.0.0
v8 8.4.300
typescript 3.9.2
$ deno eval "import docopt from 'https://raw.githubusercontent.com/Eyal-Shalev/docopt.js/master/src/docopt.ts';"
Compile https://raw.githubusercontent.com/Eyal-Shalev/docopt.js/master/src/docopt.ts
error: TS2322 [ERROR]: Type 'unknown[]' is not assignable to type 'T[]'.
  Type 'unknown' is not assignable to type 'T'.
    'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
  return Array.prototype.flat?.apply(arr, [depth]) || (
  
    at https://raw.githubusercontent.com/Eyal-Shalev/docopt.js/master/src/docopt.ts:676:3

Here's the behavior before 1.0.0 using my test case

$ deno --version
deno 1.0.0-rc1
v8 8.2.308
typescript 3.8.3
$ deno test --unstable --allow-read
running 1 tests
test middleware-rdbms-api-auto-baas-graph Dockerfile and docker-compose.yaml Transformer ... ok (33ms)

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (34ms)

Then after installing 1.0.0 using my test case:

$ deno --version
deno 1.0.0
v8 8.4.300
typescript 3.9.2
$ deno test --unstable --allow-read
Compile file:///D:/Engineering/workspaces/git.netspective.io/information-governance-suite/specification-commons/.deno.test.ts
error: TS2322 [ERROR]: Type 'unknown[]' is not assignable to type 'T[]'.
  Type 'unknown' is not assignable to type 'T'.
    'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
  return Array.prototype.flat?.apply(arr, [depth]) || (
  
    at https://raw.githubusercontent.com/Eyal-Shalev/docopt.js/master/src/docopt.ts:676:3

Please let me know if I can help elaborate anything.

shah commented

Also, @Eyal-Shalev if it helps, I'm running this on Windows 10 on two different desktops. But, the same error does also occur when my CI tool runs the unit test in Linux.

shah commented

@Eyal-Shalev here's something else I noticed: in your test cases you seem? to be running the tests against dist/docopt.mjs and not src/docopt.ts. As you can see in my deno eval I'm calling src/docopt.ts directly in Deno. If you suggest using only dist/docopt.mjs and not the src file in my code, please let me know what import statement you'd like see us use.

shah commented

While you're thinking about the resolution, here's a quick workaround that might work -- add the following before line 676 in src/docopt.ts:

// @ts-ignore TS2322

That should remove the compiler error since it seems you're doing an untyped action anyway :-)

@shah I had to do some ugly workarounds to make it work with deno, node (commonJS) & node (ESM).
I'll try to make deno run against the typescript version, and will probably use your ignore suggestion.

@shah I've released a new version with a fix.
Can you check to see that it works for you as well.

shah commented

@Eyal-Shalev thanks for the quick fix. It works for me now, using the src/docopt.ts approach directly.