Sharing classes between multiple TypeScript files using Node.JS Tools for Visual Studio - node.js

I am currently working on a Node.JS project written in TypeScript using Node.JS Tools for Visual Studio (NTVS). I have a few classes and enums spread out in 3 or 4 files in my project. I am now trying to use the classes defined in those files from my main app file. From my previous work with Node, I know that I would normally need a require call to import each other file/class if I were working with a text editor and the command-line compiler. But, if I open any TypeScript file in my project and start typing the name of a class defined in a different file, Visual Studio shows IntelliSense autocomplete for the class name and its members. This makes me think that the NTVS and/or TypeScript configuration are automatically making all of my classes available project-wide. But if I click the 'run' button, errors are printed to the console because Node can't find the referenced classes at runtime.
This behavior leads me to believe that IntelliSense isn't actually telling me that the classes are available, just that they exist (which seems odd). If I add a require call to the top of the file, and use that imported value instead of the original class name, Node finds the class and I can use it in my code. But this presents two problems:
I must come up with a new name to use for the variable that I import the class into. If I require() it with the original name, Visual Studio shows errors saying that the identifier is a duplicate, because it seems to believe that the original class is available project-wide.
I don't get the autocomplete or type checking in my usage of the class. This pretty much defeats the purpose of using TypeScript.
So, what's the proper way to do this import? Is there a way to make all my classes available globally? If not, what import statements do I need?

This behavior leads me to believe that IntelliSense isn't actually telling me that the classes are available, just that they exist
unless you have top level import or export statement the file is considered a global module and is available project wide : http://basarat.gitbooks.io/typescript/content/docs/project/modules.html
A global module will not work at runtime in node.js
You should use file level modules using import/export and compile with --module commonjs

Related

What's the difference between styled-components and styled-components/macro

Sometimes I see slightly different imports
import styled, { withTheme } from "styled-components/macro";
import styled, { withTheme } from "styled-components";
Since they have the same functionality I cannot understand the difference between them neither can google anything that could help.
In production styled-components generates unique hashes for css classes like .eeZmbc or .ZzNLl. These are used for saving space but are not useful for developers in development.
For semantic class names in development a babel plugin exists. It generates names like .Navbar__Item-sc-14eztoj-1 as in .FileName__StyledComponent-generatedHash to help us trace an element/style back to its source.
So if you use create-react-app you can use this plugin without ejecting and without adding it to babel config. You just have change your import from styled-components to styled-components/macro. A quick find-and-replace in you IDE will do the trick.
All the compile-time code transformation is handled by babel-plugin-macros
babel-plugin-macros defines a standard interface for libraries that want to use compile-time code transformation without requiring the user to add a babel plugin to their build system (other than babel-plugin-macros, which is ideally already in place).
Personally I add the babel plugins to the config file manually and use standard imports like styled-components.

Typedoc: document only things exported by a specific file

I am developing two npn packages:
https://github.com/euberdeveloper/mongo-scanner
https://github.com/euberdeveloper/mongo-cleaner
I want to document them by using typedoc. The index.js file is the one provided by the npm module and is obtained by index.ts.
I want to document only what is exported by the library index.ts. I can filter the non-exported things with the typedoc options.
The problem is that the index.ts import things from other TS files and export only some of them. For instance, it imports and re-exports some interfaces from the interfaces/index.ts and the errors classes from errors/index.ts, but imports without exporting the class Database from the utils/database/index.ts.
Being actually exported, things such that Database class are documented even if they are exported by a file different from index.ts.
How can I filter them?
The current #next version of typedoc has a --library mode. Seems that there are going to be big changes with the #beta version and the future 1.0.0 version, where the mode option will even disappear. Development seems to proceed extremely slow, so I think that the only way now is to use the (probably bugged) #next and #beta versions and wait.
Edit: the 0.20.x is no more beta and has been released, it works good, so the problem is solved

VS CShellManager not declared

I have downloaded an API to further develop it and this error appears: C2065 CShellManager not declared. I guess it might be declared in a library which is not included by default, but it should be in the zip I have downloaded. How can a look for the declaration of this class in order to know the file in which is declared and to include this library in the source code?
The CShellManager class is a part of MFC framework. It is defined in afxshellmanager.h header file.
This class was introduced in VS 2008 as part of so called MFC Feature Pack.
Basically it is only required if your project uses CMFCShellTreeCtrl and such. If this is the case then you need to call CWinAppEx::InitShellManager() in OnInitInstance() of your app class to initialize the instance of the Shell Manager.

FlashDevelop - Adding Classpath in Haxe project: 'Class not found'

I want to simply create a reusable "library" for all my future projects that I'm going to be doing in Haxe. I understand there aren't library projects in Haxe, but rather you would just have a collection of source files somewhere and import them as needed. Right?
I've created a new project using Flambe (a Haxe framework) and opened it in the FlashDevelop IDE. It compiles and runs fine.
Now I want to include my library, so I go into the Project Properties under the "Classpaths" tab and set the relative path to my library. It shows up correctly in the "References", and even has the proper code completion when I type "import ...", yet when I compile it fails on the import line stating: 'Class not found : mlg.Engine'
(mlg being the package, and Engine being the class/type)
Is there anything I'm missing?
I think (i may be wrong) that flashdevelop "references" are just autocompletion and not actually passed to the compiler.
I'm not sure what's the "right" way to do it, but I can tell you what I've done (I made a few helper classes for flambe too :P): I simply created a "fake" haxelib, I created HaxeToolkit/haxe/lib/[name]/git, and in [name] i created a .current file that contains "git".
Then on flashdevelop you have to add it as a library (Project settings -> Compiler options -> Libraries).
Note: there are probably other/better ways to do it.

Mixing typescript definition files with nodejs require over multiple files in an internal module

Are there any known issues with mixing nodejs modules (require) with typescript definition files (d.ts) multiple times over files within a module?
My scenario is that I have a module namespace per folder (much like I would in C#), then I basically compile them all via tsc to an outputted my-module.js. However I keep getting really odd errors like Could not find type HTMLElement but lots of people have pointed out that tsc includes the typescript lib file by default which contains all those types.
I have noticed a few people having odd errors when they are including the same d.ts files over multiple files which are all compiled with the --out flag to get it all into one file, so could this be causing my issues?
An example of my usage would be:
///<reference path="path/to/knockout.d.ts" />
import ko = require("knockout");
This would then be put in each file which requires knockout js, which is at least 10 files in the module i'm trying to compile currently. It just bombs out constantly saying knockout.d.ts cannot find the type HTMLElemet, Element, Document etc.
If you are using external modules (which you are if you have a top level "import" - as shown above), then you can't use the --out switch to combine multiple source files. It is a limitation that with external modules that one source file = one module. With source that is not in an external module (i.e. contributes to the 'global' scope), you can combine input source to one output JavaScript file using --out.
I have no idea about the "could not find HTMLElement" issues. If you can provide a repro (and outline which version you are using) I can take a look.

Resources