【问题标题】:Can't import JQuery into TypeScript file无法将 JQuery 导入 TypeScript 文件
【发布时间】:2019-06-01 16:44:24
【问题描述】:

安装 jquery@types/jquery 后,我似乎无法让 JQuery 在我的打字稿文件中工作。

如果我使用:

import * as $ from 'jquery';
$("#test").addClass("className");

我在$("#test") 收到以下错误:

TS2349: Cannot invoke an expression whose type lacks a call signature.

如果我按照 WebStorm 的建议将导入更改为:

import $ from 'jquery';

然后没有错误显示,但是在尝试编译文件时出现错误:

error TS1192: Module '"C:/Users/Home/Desktop/react/hello-world/node_modules/@types/jquery/index"' has no default export.

我查看了十几个其他 StackOverFlow 帖子,但没有任何帮助。有什么想法吗?

【问题讨论】:

标签: jquery typescript


【解决方案1】:

尝试将allowSyntheticDefaultImports 标志添加到您的tsconfig--allowSyntheticDefaultImports 到您的脚本中。

另一种选择是尝试esModuleInterop 标志。

参考资料:

您需要 TypeScript 1.8.0 或更高版本

【讨论】:

  • 恐怕不行。我能够从裸create-react-app 复制它,安装jquery,在index.tsx 中使用import * as $ from 'jquery';(或任何其他变体)导入并使用tsc --jsx react-native *tsx 编译。总是有一些错误。
猜你喜欢
  • 2017-10-02
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 2018-04-10
  • 1970-01-01
  • 2019-08-01
  • 2016-05-10
相关资源
最近更新 更多