我无法通过“导入”命令导入choices.js

时间:2019-02-23 14:02:04

标签: typescript

我无法通过“导入”命令导入choices.js:

import Choices from 'choices.js';

TS2307: Cannot find module 'choices.js'

这不起作用:

import Choices from '../../node_modules/choices.js/public/assets/scripts/choices.js';

TS7016: Could not find a declaration file for module '../../node_modules/choices.j
s/public/assets/scripts/choices.js'. '/project/node_modules/choices.js/public/assets/scr
ipts/choices.js' implicitly has an 'any' type.

这也是:

/// <reference path="../../node_modules/choices.js/types/index.d.ts" />
import Choices from '../../node_modules/choices.js/public/assets/scripts/choices.js';


TS7016: Could not find a declaration file for module '../../node_modules/choices.j
s/public/assets/scripts/choices.js'. '/project/node_modules/choices.js/public/assets/scr
ipts/choices.js' implicitly has an 'any' type.

仅此命令有效:

const Choices = require(`choices.js`);

但是我对loadash并没有这个问题,并且导入时没有任何问题:

yarn add -D lodash @types/lodash

import lodash from 'lodash';

我做错了什么,但我可以理解,我在哪里犯错误以及如何解决。 :(

更新: 仅当我在tsconfig.json中将noImplicitAny从true设置为false并在我的.ts文件中的该库中使用js文件的绝对路径时,es6导入才为我工作:

import * as Choices from '../../node_modules/choices.js/public/assets/scripts/choices.js';

我的版本类型有问题...

2 个答案:

答案 0 :(得分:0)

导入JS文件后,只需添加...

从“ choices.js”导入选择;

声明var选择:任意;

答案 1 :(得分:0)

我找到了解决问题的方法:这只是必要的 to set "moduleResolution": "node" in my tsconfig.json, 那么相对导入就不会开始起作用。

仅当我写以下内容时,此软件包的package.json中类型的路径才有效:

import Choices from 'choices.js';

不是:

import  Choices from 'choices.js/public/assets/scripts/choices.js';

Karol Majewski,谢谢您的帮助。你把我推到了决定。