#Typescript ambient types not working for imports
24 messages · Page 1 of 1 (latest)
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.
I have been trying to find a solution where I don't really need to import types, and declare the ones I need globally.
Why not import types?
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
Maybe try putting your types alongside the code that uses them.
you mean like creating .d.ts for specific folders?
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.
but I need to use certain types in multiple folders like Request from express
Sure, if something is shared, it can be exported and reused.
And for stuff like express, I'd just import it.
yeah, that's what I am trying to avoid, constant imports and exports for the same stuff
I have seen this work somewhere but am having a hard time finding it exactly.
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.
why so?
isn't that cleaner code wise?
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.