Deno: @ts-expect-error breaking app.

Created on 9 Aug 2020  路  11Comments  路  Source: denoland/deno

[Details]
I have a transitive dependency that is using https://deno.land/[email protected]/node/_util/_util_promisify.ts which has @ts-expect-error.

image

When using strict=true, it throws the same errors, and also, oak 6.0.1 breaks.
image

This was solved before but now it started happening again in STD with the exact same behavior. This was solved here: https://github.com/denoland/deno/issues/6033 by @kitsonk in https://github.com/denoland/deno/pull/6038 .

[Environment]
deno 1.2.3
v8 8.6.334
typescript 3.9.2

[Additional]
This behavior started happening in std >= 0.63.0 as it is not happening in std 0.62.0

bug good first issue

Most helpful comment

Oops, you're right. The comment in the ts-expect-error directive was wrong/outdated about what the error was. Here's the correct casting:

-  // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
-  if (original[kCustomPromisifiedSymbol]) {
-    // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
-    const fn = original[kCustomPromisifiedSymbol];
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  if ((original as any)[kCustomPromisifiedSymbol]) {
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
+    const fn = (original as any)[kCustomPromisifiedSymbol] as Function;

All 11 comments

lll grab this issue

The fix for this seems to be pretty easy, but Id like to be able to recreate it for I do that.

What are the actual steps to recreate this? I created a file that has https://deno.land/[email protected]/node/_util/_util_promisify.ts as a dependency, but did not see any errors on run or bundle commands.

@JayHelton I can鈥檛 tell of an specific way to recreate it because it鈥檚 happening at Mandarine鈥檚 core when upgrading to std 0.64.0. I鈥檒l investigate if I can isolate this in an example.

Like last time, it's happening probably because you have a weakened tsconfig which supresses the expected error. The fix is this replacement:

-  // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
-  const argumentNames = original[kCustomPromisifyArgsSymbol];
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  const argumentNames = original[kCustomPromisifyArgsSymbol as any];

but you'll probably have to do similar things at other @ts-expect-errors. We should just stop using that directive IMO. @kitsonk?

@nayeemrmn That may be it.

Mandarine's tsconfig:



{
    "compilerOptions": {
        "strict": false,
        "noImplicitAny": false,
        "noImplicitThis": false,
        "alwaysStrict": false,
        "strictNullChecks": false,
        "strictFunctionTypes": true,
        "strictPropertyInitialization": false,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "allowUmdGlobalAccess": false,
    }
}

We should just stop using that directive IMO.

But we shouldn't replace it with @ts-ignore either though. It is only for the very very very rare instance where we can't coerce the type system any other way, and we should use @ts-expect-error in those cases.

Like last time, it's happening probably because you have a weakened tsconfig which supresses the expected error. The fix is this replacement:

-  // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
-  const argumentNames = original[kCustomPromisifyArgsSymbol];
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  const argumentNames = original[kCustomPromisifyArgsSymbol as any];

but you'll probably have to do similar things at other @ts-expect-errors. We should just stop using that directive IMO. @kitsonk?

error: TS7053 [ERROR]: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Function'.

@nayeemrmn @kitsonk
I dont think we can just cast to an any and es ignore it. Typescript still doesnt support indexing by a symbol, or even any.

Im of the opinion that we should just use @ts-ignore for this use case, and using @ts-expect-error just causes issues for those who use typescript, but dont use a strict tsconfig (which is totally fine, imo). Id rather some lines to ignore the error, instead of coercing the type system in an arguably more complicated and hard to read way. Im sure its possible, im just not sure how succinct it would be.

Typescript still doesnt support indexing by a symbol, or even any.

@JayHelton Why wouldn't it work for any? I'm pretty sure I tried it before suggesting.

Typescript still doesnt support indexing by a symbol, or even any.

@JayHelton Why wouldn't it work for any? I'm pretty sure I tried it before suggesting.

error: TS7053 [ERROR]: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Function'.
  if (original[kCustomPromisifiedSymbol as any]) {
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

here is the code that produces the error:

// eslint-disable-next-line @typescript-eslint/no-explicit-any
  if (original[kCustomPromisifiedSymbol as any]) {

I believe typescript only supports indexing by string and number.

Oops, you're right. The comment in the ts-expect-error directive was wrong/outdated about what the error was. Here's the correct casting:

-  // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
-  if (original[kCustomPromisifiedSymbol]) {
-    // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
-    const fn = original[kCustomPromisifiedSymbol];
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  if ((original as any)[kCustomPromisifiedSymbol]) {
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
+    const fn = (original as any)[kCustomPromisifiedSymbol] as Function;

Fixed in #7024.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benjamingr picture benjamingr  路  3Comments

somombo picture somombo  路  3Comments

ry picture ry  路  3Comments

zugende picture zugende  路  3Comments

justjavac picture justjavac  路  3Comments