javascript - 找不到隐含“任何”类型的模块的声明文件 - 在导入部分 CommonJS 模块时
问题描述
目前,该项目有一个 CommonJS 模块,用于存储仅在一个文件中维护的配置值:
module.exports = {
classA: `classA`,
classB: `classB`
classC: `classC`
}
这允许稍后通过执行以下操作重用它们来生成 JS 选择器:
const CLASSES = require('./classes')
const SELECTORS = Object.keys(CLASSES).reduce((obj, key) => {
const selectorKey = key.replace(`class`, `selector`)
obj[selectorKey] = `.${CLASSES[key]}`
return obj
}, {})
module.exports = SELECTORS
选择器稍后也会在代码逻辑中使用,并且还会传递给 PostCSS 配置,后者会生成要在.pcss
文件中使用的变量列表。
这一切都非常方便,有助于开发流程,此外,CommonJS 允许在需要时仅将特定值导入组件,例如:
import { classA } from './path/classes
它像梦一样工作,它仍然如此,但是,在打字稿文件中以相同的方式导入它不满足 tslint 约束,它会发出错误警告
Could not find a declaration file for module implicitly has an 'any' type
::
这是tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noImplicitThis": true,
"baseUrl": ".",
"types": [
"webpack-env",
"jest"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
所以我想知道是否有一种有效的方法来取悦 TS 大神并以某种方式声明模块类型,或者至少在保留当前功能的同时重写呈现的逻辑。
PS:与鸭子交谈后,我得出结论,将其重写为 ts 模块将防止在 postcss 配置中要求选择器的可能性,因为它不支持 ES6import
解决方案
我找到的解决方案是向文件添加"allowJs": true,
选项tsconfig.js
推荐阅读
- android - 同一文件在不同 Android 设备上的大小是否相同?
- r - 如何显示每一行的最大值?
- recursion - 在循环中进行递归调用(对数)的函数的时间复杂度是多少?
- python - 不是 urls.py 中的有效正则表达式
- java - 使用 AssertJ Swing 测试确保预期文本输入的正确配置步骤
- mongodb - MongoDB游标不一致的行为
- ios - 如果 UICollectionView 单元格包含特定文本,则更新单元格的大小
- rxjs - RxJS 6:为什么在 BehaviorSubject 上调用值是一件坏事?(根据 no-subject-value lint 规则)
- javafx - 在活动 JDK 中找不到 JavaFX 部署库
- java - 如何使用 Environment API 从 application.properties 文件中读取 Map 值