最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use Typescript definitions to get Intellisense for my own Javascript services in VS Code? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Jul 14, 2016 at 10:44 Voreny asked Jul 14, 2016 at 9:30 VorenyVoreny 7856 silver badges13 bronze badges 3
  • github.com/bryanmacfarlane/sanenode – bryanmac Commented Aug 28, 2018 at 3:13
  • Have you tried to include your .d.ts inside your package.json? – Akash Kava Commented Aug 30, 2018 at 21:21
  • If anyone stumbles upon this question one day, the perfect answer is here : stackoverflow.com/questions/46678663/… – Telokis Commented Aug 31, 2018 at 9:09
Add a comment  | 

3 Answers 3

Reset to default 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.

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 }

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:

发布评论

评论列表(0)

  1. 暂无评论