dsherret / ts-nameof

nameof in TypeScript
MIT License
492 stars 23 forks source link

Support `typeof import` as `nameof` generic #54

Closed wh1t3cAt1k closed 5 years ago

wh1t3cAt1k commented 5 years ago

Suppose module './modules/ModuleName' exposes a non-default export named Foo.

Use case: I would like to use nameof<typeof import('./modules/ModuleName')>(x => x.Foo) to obtain the string 'Foo' without the overhead of actually importing the component, but with full compile-time path checking and type safety.

Currently it breaks as unsupported node type.

Can we enable support for this recent TS feature? It would make a lot of JS much more lightweight.

dsherret commented 5 years ago

@wh1t3cAt1k yup, I'll get on it! Thanks for bringing this to my attention.

As a temporary workaround, you can assign the type to a type alias.

type YourModule = typeof import('./modules/ModuleName');

nameof<YourModule>(x => x.Foo)
wh1t3cAt1k commented 5 years ago

@dsherret yeah, that's what I currently do! Thanks a lot!

dsherret commented 5 years ago

Should be implemented now in 3.1.0. Let me know if not.