简体   繁体   English

Webpack 抱怨打字稿定义文件导入 - 除非我添加一个 `d.ts` 结尾

[英]Webpack complains of a typescript definition file import - unless I add a `d.ts` ending

I have the weirdest situation where my webpack build complains of a single instance of importing a file where it cannot find the file, unless I add a .d.ts or .d ending in the the import literal.我遇到了最奇怪的情况,我的 webpack 构建抱怨导入文件的单个实例,但它找不到文件,除非我添加以导入文字结尾的.d.ts.d

ERROR in ./src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx
Module not found: Error: Can't resolve '../../types/schedule-type-interfaces' in '/home/myuser/dev/nicecorp/nicecorp-frontend/src/screens/homeward-questionnaire-schedule'
 @ ./src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx 33:0-66 380:10-20 384:10-20 388:10-20 392:10-20 396:10-20 400:10-20 404:10-20 408:10-20 412:10-20 416:10-20
 @ ./src/components/one-offs/MainGuiLayout.tsx
 @ ./src/components/one-offs/AppWrapper.jsx

The file is imported in 3 files, and it is only an issue in this single file:该文件是在3个文件中导入的,并且仅在此单个文件中存在问题:

$ git grep schedule-type-interfaces
src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx:import { ButtonProp, Question, QuestionId } from '../../types/schedule-type-interfaces'
src/screens/homeward-questionnaire-schedule/QuestionSelector.tsx:import { Question } from '../../types/schedule-type-interfaces'
src/utils/questionnaire-util.ts:import { ButtonProp, Question } from '../types/schedule-type-interfaces'

Adding this single diff will make the build pass with flying colors:添加这个单一的差异将使构建通过出色的颜色:

$ git diff
diff --git a/nicecorp-frontend/src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx b/nicecorp-frontend/src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx
index 0a2a3e366..4d3def526 100644
--- a/nicecorp-frontend/src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx
+++ b/nicecorp-frontend/src/screens/homeward-questionnaire-schedule/HomewardQuestionnaireScheduleScreen.tsx
@@ -32,7 +32,7 @@ import { Encounter, Patient, QuestionnaireSchedule } from '../../types/domain-ty
 import { CancellationCriteriaEnum } from '../../types/enums'
 import { nicecorpError } from '../../types/other-types'
 // TODO: find out why we need the extension for webpack not to fail
+import { ButtonProp, Question, QuestionId } from '../../types/schedule-type-interfaces.d'
-import { ButtonProp, Question, QuestionId } from '../../types/schedule-type-interfaces'
 import { connectWithDataLoader } from '../../utils/composer'
 import {
   dayNameMap,

This makes no sense to any of us, so I am reaching out in hope of becoming smarter.这对我们任何人来说都没有意义,所以我伸出手希望变得更聪明。 So it can find the file no problem if skipping the *.ts ending, but I have to specify *.d.ts for it to not fail.因此,如果跳过*.ts结尾,它可以毫无问题地找到文件,但我必须指定*.d.ts才能使其不会失败。 So weird.太奇怪了。 This is the directory in which the file lives - no name clashes:这是文件所在的目录 - 没有名称冲突:

$ ls -1 src/types/
base-type-interfaces.d.ts
base-types.js
completable-types.js
domain-type-interfaces.d.ts
domain-types.js
dom-types.js
enums.js
other-type-interfaces.d.ts
other-types.js
questions-types.js
react-specific-types.js
README.md
schedule-type-interfaces.d.ts

*.d.ts files are supposed to add typings for existing javascript code. *.d.ts文件应该为现有的 javascript 代码添加类型。 We simply used them for creating interfaces, for some weird historical reasons, and people forgot why in the process, creating new ones.我们只是用它们来创建界面,出于一些奇怪的历史原因,人们在这个过程中忘记了为什么,创建新的。

Renaming these files to *.ts was what fixed the issue.将这些文件重命名为*.ts是解决问题的方法。

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

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