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

javascript - Reference a TS interface from jsDoc - Stack Overflow

programmeradmin3浏览0评论

Using VSCode is almost possible to have the benefits from Typescript in plain .js thanks to jsDoc notation and a tsconfig.json file:

{
  "compileOnSave": false,
  "compilerOptions": {
    "noEmit": true,
    "allowJs": true,
    "checkJs": true,
    "target": "es6",
    "resolveJsonModule": true,
    "moduleResolution": "node",

  },
  "include": ["index.js", "./src"]
}
/**
 * @return {Promise<string>}
 */
const foo = Promise.resolve('hi');
module.exports = foo;

Now, is it possible to reference an interface defined in a d.ts at node_modules? in particular I'm returning a -let's call- "my_dependency.Storage" object but I'm unable to reference it using plain javascript:

/**
 * @param {Storage} storage
 */
const goo = storage => ...

will understand that I'm refering to Web Storage API from lib.dom.d.ts

  • The equivalent typescript would be:
import {Storage} from "my_dependency"
  • I've tried using triple slash directives unsuccessfully
///<reference path="node_modules/my_dependency/lib/index.d.ts" />
  • I'm expecting something like (pseudo-code)
/**
 * @param {my_module.Storage} storage
 */

Using VSCode is almost possible to have the benefits from Typescript in plain .js thanks to jsDoc notation and a tsconfig.json file:

{
  "compileOnSave": false,
  "compilerOptions": {
    "noEmit": true,
    "allowJs": true,
    "checkJs": true,
    "target": "es6",
    "resolveJsonModule": true,
    "moduleResolution": "node",

  },
  "include": ["index.js", "./src"]
}
/**
 * @return {Promise<string>}
 */
const foo = Promise.resolve('hi');
module.exports = foo;

Now, is it possible to reference an interface defined in a d.ts at node_modules? in particular I'm returning a -let's call- "my_dependency.Storage" object but I'm unable to reference it using plain javascript:

/**
 * @param {Storage} storage
 */
const goo = storage => ...

will understand that I'm refering to Web Storage API from lib.dom.d.ts

  • The equivalent typescript would be:
import {Storage} from "my_dependency"
  • I've tried using triple slash directives unsuccessfully
///<reference path="node_modules/my_dependency/lib/index.d.ts" />
  • I'm expecting something like (pseudo-code)
/**
 * @param {my_module.Storage} storage
 */
Share Improve this question asked May 18, 2020 at 10:11 Manu ArteroManu Artero 10.3k8 gold badges67 silver badges77 bronze badges 2
  • 2 In some cases the import('firebase').app.App, this is feature from TS 2.9. It might help your cause. I used it in my JS/TS project once. – Akxe Commented May 18, 2020 at 10:18
  • @Akxe didn't get what you mention until now. This is exactly the syntax I was looking for. Thanks. Going to answer my own question with a piece of code – Manu Artero Commented May 28, 2020 at 8:21
Add a comment  | 

1 Answer 1

Reset to default 26

As @Akxe mention this is how you can do this for referring a type from a lib:

/**
 * @param {import("my_dependency").Storage} storage
 */
const goo = storage => ...

Now, I found myself repeating that statement over and over again so I created an ambient alias as following:

types.d.ts

declare namespace MyProject {
  type St = import("my_dependency").Storage; // alias: reference my_dependency.Storage from plain js code
}

index.js

/// <reference path="types.d.ts"/>
...

src/foo.js

/**
 * @param {MyProject.St} storage
 */
const goo = storage => ...

==EDIT==

Here is a post at dev.to that expands this topic

发布评论

评论列表(0)

  1. 暂无评论