【问题标题】:How to import js-modules into TypeScript file?如何将 js 模块导入 TypeScript 文件?
【发布时间】:2017-05-04 07:47:52
【问题描述】:

我有一个包含这样一个文件的量角器项目:

var FriendCard = function (card) {
    var webElement = card;
    var menuButton;
    var serialNumber;

    this.getAsWebElement = function () {
        return webElement;
    };

    this.clickMenuButton = function () {
        menuButton.click();
    };

    this.setSerialNumber = function (numberOfElements) {
        serialNumber = numberOfElements + 1;
        menuButton = element(by.xpath('.//*[@id=\'mCSB_2_container\']/li[' + serialNumber + ']/ng-include/div/div[2]/i'));
    };

    this.deleteFriend = function () {
        element(by.css('[ng-click="deleteFriend(person);"]')).click();
        element(by.css('[ng-click="confirm()"]')).click();
    }
};
module.exports = FriendCard;

文件的路径是./pages/FriendCard.js

使用require() 将其导入另一个文件没有问题:

var FriendCard = require('./../pages/FriendCard');

所以,我决定像这样将该文件导入 TypeScript 文件:

import {FriendCard} from './../pages/FriendCard'

我正在使用 WebStorm。它告诉我 (TS2305) 它没有导出的成员 'FriendCard'。

也许我必须以某种方式配置 tsconfig.json 文件,但我仍然不知道它是如何工作的。你能帮帮我吗?

【问题讨论】:

  • “我正在使用 WebStorm,所以它告诉我,(TS2305)它没有导出的成员 'FriendCard'。” 只是一个小注释 -- TS2305意味着警告/错误是由实际的 TypeScript 编译器/语言服务产生的,而不是实际的 WebStorm .. 因为 IDE 在他们自己的检查/解析器中不使用这样的编号。

标签: javascript typescript protractor webstorm


【解决方案1】:

您可以按如下方式导入整个模块:

import * as FriendCard from './../pages/FriendCard';

更多详情请参考Typescript官方文档modules部分。

最近更新的解决方案:我们需要调整 tsconfig.json 以允许 JS 模块导入。 归功于@paulmest、@ben-winding @crispen-gari 解决方案。

{
  "compilerOptions": {
    "allowJs": true
  }
}

【讨论】:

  • 感谢您在实际相对路径前添加“./”。这使得编译时路径解析的所有差异。
  • 这是最新的吗?我仍然遇到同样的错误...找不到模块的声明文件...
  • @nathan-h 你可能需要修改 tsconfig 文件...更多信息在这里:stackoverflow.com/a/56909179/872328
【解决方案2】:

我目前正在使用一些遗留代码库并引入最少的 TypeScript 更改,看看它是否对我们的团队有帮助。根据您希望对 TypeScript 的严格程度,这可能适合您,也可能不适合您。

对我们最有帮助的方法是使用此属性扩展我们的 tsconfig.json 文件:

// tsconfig.json excerpt:

{
  ...
  "compilerOptions": {
    ...
    "allowJs": true,
    ...
  }
  ...
}

此更改允许编译具有 JSDoc 类型提示的 JS 文件。我们的 IDE(JetBrains IDE 和 VS Code)也可以提供代码补全和 Intellisense。

参考资料:

【讨论】:

  • 我已经把 compilerOptions ``` lang-js { "compilerOptions": { ..., "allowJs": true } ... } ```
【解决方案3】:

在你的第二个陈述中

import {FriendCard} from './../pages/FriendCard'

您告诉 typescript 从文件 './pages/FriendCard' 中导入 FriendCard

您的 FriendCard 文件正在导出一个变量,并且该变量正在引用匿名函数。

这里有两个选择。如果您想以类型化的方式执行此操作,您可以重构要键入的模块(选项 1),或者您可以导入匿名函数并添加 d.ts 文件。有关详细信息,请参阅https://github.com/Microsoft/TypeScript/issues/3019。关于为什么需要添加文件。

选项 1

重构要键入的好友卡片 js 文件。

export class FriendCard {
webElement: any;
menuButton: any;
serialNumber: any;

constructor(card) {
    this.webElement = card;
    this.menuButton;
    this.serialNumber;
}



getAsWebElement = function () {
    return this.webElement;
};

clickMenuButton = function () {
    this.menuButton.click();
};

setSerialNumber = function (numberOfElements) {
    this.serialNumber = numberOfElements + 1;
    this.menuButton = element(by.xpath('.//*[@id=\'mCSB_2_container\']/li[' + serialNumber + ']/ng-include/div/div[2]/i'));
};

deleteFriend = function () {
    element(by.css('[ng-click="deleteFriend(person);"]')).click();
    element(by.css('[ng-click="confirm()"]')).click();
}
};

选项 2

可以导入匿名函数

 import * as FriendCard from module("./FriendCardJs");

d.ts 文件定义有几个选项。这个答案貌似最全:How do you produce a .d.ts "typings" definition file from an existing JavaScript library?

【讨论】:

    【解决方案4】:

    2021 解决方案

    如果您仍然收到此错误消息:

    TS7016: Could not find a declaration file for module './myjsfile'
    

    那么您可能需要将以下内容添加到tsconfig.json

    {
      "compilerOptions": {
        ...
        "allowJs": true,
        "checkJs": false,
        ...
      }
    }
    

    这可以防止 typescript 尝试将模块类型应用于导入的 javascript。

    【讨论】:

    • 救命稻草! (记得重启你的编辑器)
    【解决方案5】:

    我测试了 3 种方法来做到这一点...

    方法一:

          const FriendCard:any = require('./../pages/FriendCard')
    

    方法二:

          import * as FriendCard from './../pages/FriendCard';
    

    方法3:

    如果你能在 tsconfig.json 中找到类似的东西:

          { "compilerOptions": { ..., "allowJs": true }
    

    然后你可以写: import FriendCard from './../pages/FriendCard';

    【讨论】:

      【解决方案6】:

      我一直面临这个问题,但这解决了我的问题进入 tsconfig.json 并在 compilerOptions 下添加以下内容>

      {
        "compilerOptions": {
            ...
            "allowJs": true
            ...
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-16
        • 2018-08-13
        • 1970-01-01
        • 2019-05-09
        • 2020-06-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        相关资源
        最近更新 更多