简体   繁体   中英

unexpected identifier in .d.ts file

I am using typescript with the following build instructions.

"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. When running npm start I get the following error. in the .d.ts file

\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.

the content of collection1.model.d.ts is as follows. which is generated by tsc --emitDeclarationOnly

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

tsconfig.json

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

}

.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. Thank you to all that helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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