#Organizing Types

7 messages · Page 1 of 1 (latest)

green otter
#

I am still new to TS but love it because it is making me code better. My current topic of research is how to organize some types I have.

Background: I am creating a Discord bot; I have several interfaces that define how various components are shaped, like Command, ScheduledEvent, GatewayEvent, etc. These interfaces are then consumed in multiple files; for example, each command is in a file.

So, with that, I am trying to figure out the best way for defining/declaring those interfaces. I have read a bunch, but not seeing much more than just basically opinon...which may be the only answer lol.

Is it acceptable to place these in a types file in a declare global block? Or should I import them in each place consumed? I know d.ts files are usually for JS libraries, but is it acceptable to use in something like this?

digital swallow
#

generally, don't mix d.ts and ts.

#

.ts.js + .d.ts

#

the usual recommendation i've seen is to treat them like any other entity used across multiple files, to declare where it's most relevant and to import from there. with that, you get a clear indication of where the type is from, which helps if you have similarly named or duplicate types.

green otter
#

Ok, so putting them in like a declare global {} block is a bad idea?

digital swallow
#

not in and of itself, but afaik it's usually not recommended over the approach i mentioned

#

there's no inherent problem with it, so if it resonates with you and it's a personal project, go ahead