tapable icon indicating copy to clipboard operation
tapable copied to clipboard

AsyncHook in types

Open watjurk opened this issue 3 years ago • 6 comments

AsyncHook is declared in types but not exported by lib, this leads to types errors when trying to instantiate new AsyncHook.

error TypeError: tapable.AsyncHook is not a constructor

I would propose remove AsyncHook from tapable.d.ts because it's misleading. Thanks!

watjurk avatar Apr 24 '21 21:04 watjurk

TypeError is not related to types, you have wrong code

alexander-akait avatar Apr 26 '21 12:04 alexander-akait

I believe that's not true. AsyncHook is declared by tapable.d.ts and when compiling code like this:

import { AsyncHook } from "tapable";

const someHook = new AsyncHook();

Typescript throws no error, but at runtime module tapable does not export AsyncHook and we get TypeError. We are declaring AsyncHook in the types so typescript compiles no problem but when we try to run it there is no AsyncHook exported by tapable. Do you see the problem now?

watjurk avatar Apr 26 '21 13:04 watjurk

There is not AsyncHook export, check it https://github.com/webpack/tapable/blob/master/tapable.d.ts

alexander-akait avatar Apr 26 '21 13:04 alexander-akait

Oh you are right. There seams to be some "feature" of typescript which automatically exports declared stuff. The only "workaroud" I've found is to declare the "private" types in separate file and then import them in tapable.d.ts.

I think that this should be done in this case because importing not existing class and not getting error from typescript, and getting an TypeError on runtime is very confusing.

watjurk avatar Apr 26 '21 13:04 watjurk

Why you need AsyncHook?

alexander-akait avatar Apr 26 '21 13:04 alexander-akait

No I don't need it, I meant that the fact that I'm able to import it (at least it types) from tapable is very confusing.

watjurk avatar Apr 26 '21 13:04 watjurk