简体   繁体   English

当 index.d.ts 存在时,TypeScript 不会导入 index.js

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

I'm having strange behavior in TypeScript right now.我现在在 TypeScript 中有奇怪的行为。 In a folder separate from my source folder I have generated JS (protobufjs) with type definitions.在与我的源文件夹分开的文件夹中,我生成了带有类型定义的 JS(protobufjs)。 When I try to import the index file from that folder I get the error [foldername]/index.d.ts is not a module .当我尝试从该文件夹导入索引文件时,出现错误[foldername]/index.d.ts is not a module This even happens if I explicitly import [folder]/index or even [folder]/index.js .如果我明确导入[folder]/index甚至[folder]/index.js也会发生这种情况。

Any idea what could be causing this?知道是什么原因造成的吗?

tsconfig looks like this: 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" ]
}

And possibly of note, the generated file I'm importing is under proto , in the same parent folder as src , and imported via eg import * as protos from "../../proto";可能值得注意的是,我正在导入的生成文件位于proto下,与src位于同一父文件夹中,并通过例如import * as protos from "../../proto"; . .

I'm also fairly certain this worked until recently, so some configuration might have changed or a module version updated that I haven't caught as this is a team project.我也相当确定这直到最近才有效,因此某些配置可能已更改或模块版本已更新,因为这是一个团队项目,因此我没有发现。 Node 13.9.0, TypeScript 3.7.2.节点 13.9.0,打字稿 3.7.2。

I just tested this... this is my folder structure我刚刚测试了这个...这是我的文件夹结构

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

The trick was adding the path to [project root]/test/index.d.ts to my tsconfig.json file.. After that, I no longer got the error about "not a module".. I am assuming your index.d.ts has the declare module statement in it...诀窍是将[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