【问题标题】:how to import custom typings into a vscode js project如何将自定义类型导入 vscode js 项目
【发布时间】:2020-07-20 15:34:21
【问题描述】:

我有一个 js 项目,我使用 vscode。对于类型检查,我使用//@ts-check。 一个依赖项thinky 没有类型信息,但我找到了@emtypes/thinky(github repo 已删除)。

在 vscode 中,我得到一个提示,我应该使用 declare module 'thinky' 创建一个 *.d.ts 文件。这样做会消除错误,但只是将thinky 定义为any,这没有帮助。

如何从 node_modules/@emtypes/thinky/index.d.ts 导入自定义类型?

search 展示了许多如何将 js 项目导入 ts 项目的示例,但我需要相反的方式。我还找到了Is it possible to use custom type definitions in an ES6 project?,但那是关于为 js 项目创建类型,我需要为导入的 js 库导入自定义 typings

【问题讨论】:

    标签: visual-studio-code typescript-typings


    【解决方案1】:

    我在这里找到了解决方案:https://github.com/microsoft/TypeScript-Handbook/blob/722bdbec8e887aae7924c1f0d3edca48b7424e5f/pages/Type%20Checking%20JavaScript%20Files.md#import-types

    在我使用thinky的文件中,我添加了:

    /**
     * @typedef { import('@emtypes/thinky').Thinky } thinky
     */
    
    /** @type {thinky} */
    let thinky
    

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 2020-12-03
      • 2013-08-17
      • 2018-03-22
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多