webpack / tapable

Just a little module for plugins.
MIT License
3.71k stars 393 forks source link

AsyncHook in types #159

Open watjurk opened 3 years ago

watjurk commented 3 years ago

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!

alexander-akait commented 3 years ago

TypeError is not related to types, you have wrong code

watjurk commented 3 years ago

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?

alexander-akait commented 3 years ago

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

watjurk commented 3 years ago

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.

alexander-akait commented 3 years ago

Why you need AsyncHook?

watjurk commented 3 years ago

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.