我使用Typescript处理NodeJS项目,但无法使其与autocomplete和CommonJS一起使用。 这是我的配置:
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"lib": [],
"allowJs": true,
"declaration": true,
"declarationDir": "./dist/typings/",
"outDir": "./dist/",
"strict": true,
"alwaysStrict": true,
"moduleResolution": "node",
"rootDirs": ["src"],
"esModuleInterop": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
jsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"checkJs": true,
"allowSyntheticDefaultImports": true
},
"exclude": ["node_modules"]
}
package.json
{
"name": "XXXX",
"version": "1.0.0",
"description": "",
"main": "dist/main.js",
"module": "dist/main.js",
"scripts": {
"build": "webpack",
"dev": "webpack --watch",
"test": "jest"
},
"types": "dist/typings/main.d.ts",
"keywords": [],
"author": "",
"license": "ISC",
...
}
webpack.config.js
const path = require('path');
module.exports = {
entry: path.resolve(__dirname, 'src', 'main.ts'),
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'commonjs2',
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
target: 'node',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
},
{
test: /\.m?js$/,
exclude: /(node_modules)/,
use: 'babel-loader',
},
],
},
};
我的TS代码 src / main.ts
export class Api{
private apiKey: string;
private secretKey: string;
constructor(apiKey: string, secretKey: string) {
this.apiKey = apiKey;
this.secretKey = secretKey;
}
public test() {}
}
export namespace Api{
export interface IRequestParameters {}
export enum ERequestMethod {
POST = 'POST',
GET = 'GET',
PUT = 'PUT',
DELETE = 'DELETE',
}
}
当我尝试导入构建JS时,代码正在运行,但是VSCode上没有自动完成功能:
const Api = require('../dist/main');
const api = new Api(process.env.API_KEY, process.env.SECRET_KEY);
你有什么主意吗?我尝试了很多选择...