简体   繁体   English

无法找到隐式具有“任何”类型的模块的声明文件 - 在导入部分 CommonJS 模块时

[英]Could not find a declaration file for module implicitly has an 'any' type - while importing part of CommonJS module

Currently, the project has a CommonJS module like in order to store the configuration values which are maintained only in one file:目前,该项目有一个 CommonJS 模块,例如为了存储仅在一个文件中维护的配置值:

module.exports = {
  classA: `classA`,
  classB: `classB`
  classC: `classC`
}

This allows later to reuse them for generating JS selectors by doing:这允许稍后通过执行以下操作来重用它们来生成 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

The selectors are later also used in the code logic and are also passed to PostCSS config which generates a list of variables to be used in .pcss files.选择器稍后也用于代码逻辑,并传递给 PostCSS 配置,后者生成要在.pcss文件中使用的变量列表。

This is all super handy and helps the developing flow, plus CommonJS allows to import only specific value to the components when needed like:这一切都非常方便,有助于开发流程,此外,CommonJS 允许在需要时仅将特定值导入到组件中,例如:

import { classA } from './path/classes

It worked like a dream and it still does, however, importing it the same way in the typescript file doesn't satisfy the tslint constrains and it warns with an error: Could not find a declaration file for module implicitly has an 'any' type :它像梦一样工作,但它仍然如此,但是,在打字稿文件中以相同的方式导入它不满足 tslint 约束,并警告错误: Could not find a declaration file for module implicitly has an 'any' type

Here is the tsconfig.json这是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"
  ]
}

So I wonder if there is a valid way to please TS gods and somehow declare the module types, or at least rewrite the presented logic while preserving the current functionality.所以我想知道是否有一种有效的方法来取悦 TS 大神并以某种方式声明模块类型,或者至少在保留当前功能的同时重写呈现的逻辑。

PS: After talking with a duck I came to the conclusion that rewriting it into ts module will prevent the possibility to require the selectors in the postcss config since it doesn't support ES6 import PS:与鸭子交谈后,我得出的结论是,将其重写为 ts 模块将防止在 postcss 配置中需要选择器的可能性,因为它不支持 ES6 import

我发现的解决方案是在tsconfig.js文件中添加"allowJs": true, option

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到模块的声明文件。 '/path/to/module-name.js'隐式具有'any'类型 - Could not find a declaration file for module. '/path/to/module-name.js' implicitly has an 'any' type 找不到模块'../constants/links'的声明文件| src / constants / links.js'隐式具有'any'类型 - Could not find a declaration file for module '../constants/links' | src/constants/links.js' implicitly has an 'any' type 反应/节点构建错误。 找不到模块“历史”的声明文件。 'node_modules/history/index.js' 隐含一个 'any' 类型 - React/node build error. Could not find a declaration file for module 'history'. 'node_modules/history/index.js' implicitly has an 'any' type 使用ES6导入节点模块时找不到模块的声明文件 - Could not find a declaration file for module while importing a node module using ES6 导入现有 .jsx 文件时“找不到模块的声明文件” - "Could not find a declaration file for module" when importing existing .jsx files 找不到模块的声明文件 - Could not find a declaration file for module 无法找到模块'drivelist'的声明文件 - Could not find a declaration file for module 'drivelist' 找不到模块“mymodule”的声明文件 - Could not find a declaration file for module 'mymodule' 找不到模块“react”的声明文件 - Could not find a declaration file for module 'react' 在反应中找不到模块错误的声明文件 - Could not find a declaration file for module error in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM