#Import type in multiple files?

18 messages · Page 1 of 1 (latest)

ebon sky
#

I defined a type BotData in another file, botData.d.ts and one of its properties is a type (Collection) that was imported from another library. In another file when I am trying set all the properties of a BotData variable, when I try to do new Collection() for the field of type Collection, it says it cannot find the name. I know I can just import it into this file as well, but it feels redundant and was wondering if it is possible when I import BotData for it to also implicitly import what BotData imports. Here are some screenshots

lapis knot
#

it's possible

#

you are trying to create a new instance of a class

#

you have to import that object at runtime to acrtually create an instance of it

#

if it was a TS type, that only existed at compile time, then it would be theoretically possible, tho not recommended

#

also conside the case where mutliple classes have the same name but are in different files

#

@ebon sky

#

also, you mentionned .d.ts files
those files are meant to add TS types to actual JS files from a library, it's probably not what you are doing/need

ebon sky
#

ok, thanks. I last used ts 2 years ago for a react project in a class. So i am not aware of the proper way of doing things. When you say something is a TS type, do you mean its a type that I (or someone else) defined with TS. Also, when i define a type like I did, what would be the best way to do it, since it seems .d.ts is not the way in this case

lapis knot
#
  • if you have every worked with JS, TS is no different, except for added type annotations
#
  • if you use a JS library, and it doesn't have TS types that come with it, then you might want to create .d.ts files to add the types back yourself
#
  • otherwise, just use .ts files
#
  • if you need an object (variable / class) from another file, you need to import it explicitely
#

it's that simple

ebon sky
#

okok thanks, that makes sense. In this case it seems that I just .ts then for this type

#

and looks like I'll need to import the the type as well

#

thank you for the help

lapis knot