简体   繁体   English

导入现有 .jsx 文件时“找不到模块的声明文件”

[英]"Could not find a declaration file for module" when importing existing .jsx files

(Apologies if this is a duplicate, I can't find any Q&A's that deal with existing .jsx files) (抱歉,如果这是重复的,我找不到任何处理现有 .jsx 文件的问答)

I've been migrating my app over to typescript and have now got a load of the following errors:我一直在将我的应用程序迁移到打字稿,现在遇到了以下错误:

Could not find a declaration file for module '../components/...'. '.../components/....js' implicitly has an 'any' type.ts(7016)

I'd prefer not to have to go through my entire app right now and convert everything to .tsx .我宁愿现在不必浏览我的整个应用程序并将所有内容转换为.tsx I feel like turning noImplicitAny to false in my tsconfig file would be excessive, given that I only want to ignore the issue of imports right now.鉴于我现在只想忽略导入问题,我觉得在我的tsconfig文件中将noImplicitAnyfalse会过分。

Is there a more sensible way to resolve these errors?有没有更明智的方法来解决这些错误?

You should create the types for some packages which doesn't exist.您应该为一些不存在的包创建类型。

declare module 'objectname' {
  const content: any;
  export default content;
}

All the javascript modules may not consist of types for it.所有 javascript 模块可能不包含它的类型。 So, we need to create type definitions for the object which we define.因此,我们需要为我们定义的对象创建类型定义。 The easiest way to define for the third party is as above.为第三方定义的最简单方法如上所述。 Because of the webpack checks for the respective index.d.ts .因为 webpack 会检查相应的index.d.ts The d.ts is the type definition file for the object you will have. d.ts是您将拥有的对象的类型定义文件。

In your case, it is missing and hence you receive that error.在您的情况下,它丢失了,因此您会收到该错误。 So a type definition file has to be created with the object name.因此,必须使用对象名称创建类型定义文件。 Or it will be sometimes the extensions.或者有时会是扩展。

any refers to any datatype within the object. any指的是对象内的任何数据类型。 You must get your hands dirty with typescript interfaces and types.您必须亲自接触打字稿接口和类型。

Simple example: https://www.typescriptlang.org/play?#code/PTAEAEDMEsBsFMB2BDAtvAXKZjqubAHQAuAzgLABQ8AHgA4D2ATsaMQJ53ygDCyrAXlABvUACMm8eABMspYk2iIA5gG5Q7eMiYB5SACFoLABZZEAV1Rj4TUAF9VVWoxZtO3ACINloIaIlS0qRyCkrKANoAuuqa2nqGJmaW1rYOTvTMrADGDIjyoFmS-PB8xAByaNxCABQAlL4AfKAARJCw5pCQ7M2OlFQgEDAIKOhYAG4E0NIkFJR4LqwcXCK8-AA0oF4+dqCQTAyoLYTAOHgEhABWpD3pC27LAIK4+LCkvqusAD6b3r39YFA4EhKlhkHQ6DN-hAbPsmMFQABGADMADYEVR5pl7txRIUtMQSvwKuh7Lt9odmsdTi9Ltdejk8qwRlUCkUCaVifA6qogA简单的例子: https://www.typescriptlang.org/play?#code/PTAEAEDMEsBsFMB2BDAtvAXKZjqubAHQAuAzgLABQ8AHgA4D2ATsaMQJ53ygDCyrAXlABvUACMm8eABMspYk2iIA5gG5Q7eMiYB5SACFoLABZZEAV1Rj4TUAF9VVWoxZtO3ACINloIaIlS0qRyCkrKANoAuuqa2nqGJmaW1rYOTvTMrADGDIjyoFmS-PB8xAByaNxCABQAlL4AfKAARJCw5pCQ7M2OlFQgEDAIKOhYAG4E0NIkFJR4LqwcXCK8-AA0oF4+dqCQTAyoLYTAOHgEhABWpD3pC27LAIK4+LCkvqusAD6b3r39YFA4EhKlhkHQ6DN-hAbPsmMFQABGADMADYEVR5pl7txRIUtMQSvwKuh7Lt9odmsdTi9Ltdejk8qwRlUCkUCaVifA6qogA

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM