简体   繁体   English

.d.ts文件中的意外标识符

[英]unexpected identifier in .d.ts file

I am using typescript with the following build instructions. 我正在使用带有以下构建说明的typescript。

"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
"start": "npm run build:types && npm run build:js && node ./lib/bin/www.js"

One of my .ts files imports mongoose. 我的一个.ts文件导入了mongoose。 When running npm start I get the following error. 运行npm start出现以下错误。 in the .d.ts file .d.ts文件中

\lib\models\v1\collection1.model.d.ts:1
(function (exports, require, module, __filename, __dirname) { import mongoose from 'mongoose';
                                                                     ^^^^^^^^

SyntaxError: Unexpected identifier

note "@types/mongoose" and "mongoose" are dependence already. 注意“@ types / mongoose”和“mongoose”已经是依赖。

the content of collection1.model.d.ts is as follows. collection1.model.d.ts的内容如下。 which is generated by tsc --emitDeclarationOnly 这是由tsc --emitDeclarationOnly生成的

import mongoose from 'mongoose';
declare const _default: mongoose.Model<mongoose.Document, {}>;
export default _default;

tsconfig.json tsconfig.json

{
"compilerOptions": {
  "target": "es2015",                       
  "module": "commonjs",                     
  "declaration": true,                     
  "outDir": "./lib",                          
  "strict": false,                           
  "allowSyntheticDefaultImports": true,     
  "esModuleInterop": true,
},
"include": ["src"]

} }

.babelrc .babelrc

{
"presets": [
    "@babel/env",
    "@babel/typescript"
],
"plugins": [
    "@babel/proposal-class-properties",
    "@babel/proposal-object-rest-spread",
    "@babel/transform-runtime"
]

} }

So the problem is I should have not used babel and instead simply use tsc to generate all the code. 所以问题是我应该没有使用babel而是简单地使用tsc来生成所有代码。 Thank you to all that helped 谢谢所有有帮助的人

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

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