#Typescript ambient types not working for imports

24 messages · Page 1 of 1 (latest)

dense locust
#

I have a global.d.ts file where I am importing stuff like Request from express, I want to make them globally available but it isn't working as intended.

silent mica
#

I'm not sure the exact syntax for declaraing global types - I think you need to not have top-level imports or exports. (Maybe they can go inside declare global)

#

... but I'd mostly recommend not doing this.

#

Better to just define types (ideally where they're used) and importe/export them, rather than putting them in the global space.

dense locust
#

I have been trying to find a solution where I don't really need to import types, and declare the ones I need globally.

silent mica
#

Why not import types?

dense locust
#

It causes too much clutter, especially since I am in very early development phase and need to change stuff regularly, that usually means going to each file and changing types individually

silent mica
#

Maybe try putting your types alongside the code that uses them.

dense locust
#

you mean like creating .d.ts for specific folders?

silent mica
#

No, I actually mean putting the types in .ts files with the runtime code.

#

Having an entire separate structure for your types is generally not necessary.

dense locust
silent mica
#

Sure, if something is shared, it can be exported and reused.

#

And for stuff like express, I'd just import it.

dense locust
#

yeah, that's what I am trying to avoid, constant imports and exports for the same stuff

dense locust
silent mica
#

Okay, I think this is bad practice for all the same reasons why declaring global values is widely considered bad practice. But if you're sure, good luck.

dense locust
#

isn't that cleaner code wise?

silent mica
#

It makes the code less clear: hard to see where values come from and what are actually globals, and there can be conflict (e.g. I'm pretty sure there is a native Request type which express's type would conflict with)

#

Removing imports makes less code but it doesn't actually make it easier to understand the code.

dense locust
#

hmm

#

that makes sense.

#

didn't think about it that way. thanks!