20

I am developing a backend server using SailsJS. It basically injects all model helper services, as well as my own services into the global namespace. It would benefit me greatly if I was able to get Intellisense for those services.

I first set up typings and installed global type definitions for lodash and node. It works like a charm after creating a jsconfig.json and tsconfig.json files.

Next I wanted to create a basic definitions file for my own services. I created a directory in typings/globals with a index.d.ts file in it:

declare namespace foo {
    export function bar();
}
declare var baz: { some: number };

This is just to make sure I don't waste time writing definitions if they won't work.

Next I included that index.d.ts file in typings/index.d.ts by adding a reference tag:

/// <reference path="globals/psiewakacje/index.d.ts" />

To my surprise, it does not work in my project's Javascript files. Upon typing foo. or baz. I do not get any sensible Intellisense.

 

The only Intellisense support I was able to get was when I imported those services in every file via:

import * as InternalParser from '../services/InternalParser';

or

var InternalParser = require('../services/InternalParser');

but this doesn't use Typescript's definition files and just gives me the exported properties. Overall a not desirable result.

 

I wonder how to get it to work correctly. I looked at node's and lodash's type definition files and they do the same: declare a variable / namespace with a specific type. However, their definitions work in Javascript and mine don't. How to get it right?

3

3 Answers 3

5
+25

I can reproduce the behavior described if I create a tsconfig.json file without the "allowJs": "true" compiler option. If you want your TypeScript and JavaScript files to be considered as a single project, you should have a tsconfig.json file with "allowJs": "true" and no jsconfig.json file.

Sign up to request clarification or add additional context in comments.

7 Comments

So, if I change my jsconfig.json file to a tsconfig.json file with "allowJs": true, I will be able to code in JS and define custom typings for my js files? You can take a look at this comment to have a detailed explanation regarding what I am trying to achieve.
Yes. For typings dependent on the imported file, your .d.ts file needs to be either alongside your .js file or virtually alongside it based on the rootDirs compiler option. If you need more help meeting requirements not described in the original question, please submit a new question.
It doesn't work for me. I tried recreating the original question and applying your suggestion but I don't get any intellisense anymore.
See my working example project at github.com/mattmccutchen/stackoverflow-38370549 .
I found the absolute perfect answer here. And it doesn't require me to use a tsconfig file instead of a jsconfig.
|
2

You don't need to add a reference to typings/index.d.ts. The easiest would be to just add your declarations to a global declaration file, anywhere in your project.

Further, instead of using var and namespace, just use interface.

Eg. in the root of your directory, you can easily add

// index.d.ts
interface Foo {
  bar: () => void
}

interface Baz { some: number }

1 Comment

This doesn't work. After typing exactly this code inside a typings/index.d.ts file, I still don't get autocomplete when I type Fo followed by Ctrl + Space. Moreover, I'd like those typings to not be globals but rather dependent on the imported file. For exemple, I'd like to have a typings/Foo.d.ts that is used when I include src/models/Foo.js somewhere. Is this even possible?
2

I had the same issue and discovered that the editor (VSCode) was looking in the wrong directory for the file, problem was solved by relative referencing the correct path, the specific path will vary, but in my example it was:

/// <reference path="../../typings/index.d.ts" />

index.d.ts contains the following:

/// <reference path="globals/core-js/index.d.ts" />
/// <reference path="globals/jasmine/index.d.ts" />
/// <reference path="globals/node/index.d.ts" />

and my directory/file structure appears as follows:

enter image description here

2 Comments

For me the problem is not getting the Intellisense for custom modules, not ones installed with typings, so unfortunately this is not the solution for me. Thanks for trying, though!
Ah understood! I'll let you know if I come across anything else that might help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.