Sharing classes between multiple TypeScript files using Node.JS Tools for Visual Studio -
i working on node.js project written in typescript using node.js tools visual studio (ntvs). have few classes , enums spread out in 3 or 4 files in project. trying use classes defined in files main app file. previous work node, know need require
call import each other file/class if working text editor , command-line compiler. but, if open typescript file in project , start typing name of class defined in different file, visual studio shows intellisense autocomplete class name , members. makes me think ntvs and/or typescript configuration automatically making of classes available project-wide. if click 'run' button, errors printed console because node can't find referenced classes @ runtime.
this behavior leads me believe intellisense isn't telling me classes available, exist (which seems odd). if add require
call top of file, , use imported value instead of original class name, node finds class , can use in code. presents 2 problems:
- i must come new name use variable import class into. if
require()
original name, visual studio shows errors saying identifier duplicate, because seems believe original class available project-wide. - i don't autocomplete or type checking in usage of class. pretty defeats purpose of using typescript.
so, what's proper way import? there way make classes available globally? if not, import statements need?
this behavior leads me believe intellisense isn't telling me classes available, exist
unless have top level import
or export
statement file considered global module , available project wide : http://basarat.gitbooks.io/typescript/content/docs/project/modules.html global module not work @ runtime in node.js
you should use file level modules using import
/export
, compile --module commonjs
Comments
Post a Comment