簡體   English   中英

當 index.d.ts 存在時,TypeScript 不會導入 index.js

[英]TypeScript won't import index.js when index.d.ts is present

我現在在 TypeScript 中有奇怪的行為。 在與我的源文件夾分開的文件夾中,我生成了帶有類型定義的 JS(protobufjs)。 當我嘗試從該文件夾導入索引文件時,出現錯誤[foldername]/index.d.ts is not a module 如果我明確導入[folder]/index甚至[folder]/index.js也會發生這種情況。

知道是什么原因造成的嗎?

tsconfig 看起來像這樣:

{
  "compilerOptions": {
    "target": "es2017",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */
    "rootDir": "./src",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "composite": true,                     /* Enable project compilation */information */
    "removeComments": true,                /* Do not emit comments to output. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    "strictNullChecks": true,              /* Enable strict null checks. */
    "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */
    "noUnusedLocals": true,                /* Report errors on unused locals. */
    "noUnusedParameters": true,            /* Report errors on unused parameters. */
    "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  },
  "include": [ "src" ],
  "exclude": [ "test" ]
}

可能值得注意的是,我正在導入的生成文件位於proto下,與src位於同一父文件夾中,並通過例如import * as protos from "../../proto"; .

我也相當確定這直到最近才有效,因此某些配置可能已更改或模塊版本已更新,因為這是一個團隊項目,因此我沒有發現。 節點 13.9.0,打字稿 3.7.2。

我剛剛測試了這個...這是我的文件夾結構

_[project root]
 |_test
   |_test.js
   |_index.d.ts
 |_index.ts
 |_tsconfig.json
 |_package.json
 |_package.lock.json

訣竅是將[project root]/test/index.d.ts的路徑添加到我的tsconfig.json文件中.. 之后,我不再收到關於“不是模塊”的錯誤..我假設你的index.d.tsdeclare module語句...

// tsconfig.json
{
  "compilerOptions": {
    "target": "ESNEXT",                         
    "module": "commonjs", 
    "outDir": "./dist",
    "strict": true, 
    "esModuleInterop": true,  
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    // THIS IS WHAT FIXED IT
    "test/index.d.ts"
  ]
}
// /test/index.d.ts
declare module 'test'
// /test/test.js
function test() {
    console.log('test');
}

// Not sure if you're using `module.exports` or not
export default test;
// index.ts
import test from './test/test';

test()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM