unexpected identifier in .d.ts file

1k Views Asked by At

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"
]

}

1

There are 1 best solutions below

0
On

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