【发布时间】:2021-11-21 08:20:34
【问题描述】:
将语法从 require, CommonJS 更改为 import, ES Module 时出现错误。
我尝试使用 Node.js、TypeScript、MySQL 创建一个待办事项应用程序。
首先,我编写了以下代码。
// db.ts
export {};
const mysql = require('mysql2');
const pool = mysql.createPool({
host: 'localhost',
user: 'user',
password: 'password',
database: 'db',
});
module.exports = pool;
//index.ts
import express from 'express';
import { Express, Request, Response } from 'express';
import { QueryError, RowDataPacket } from 'mysql2';
const pool = require('./db');
const app: Express = express();
app.get("/todos", async (req: Request, res: Response) => {
await pool.promise().query(
'SELECT * FROM todo'
)
.then((rows: RowDataPacket[]) => {
res.json(rows[0]);
})
.catch((error: QueryError) => {
throw error;
})
});
这些代码运行良好。没有错误。
但是,使用 import 而不是 require 会给我带来错误。
//db.ts
import { createPool } from 'mysql2';
export const pool = createPool({
host: 'localhost',
user: 'user',
password: 'password',
database: 'db',
});
//index.ts
import express from 'express';
import { Express, Request, Response } from 'express';
import { QueryError, RowDataPacket } from 'mysql2';
import { pool } from '.db';
const app: Express = express();
app.get("/todos", async (req: Request, res: Response) => {
await pool.promise().query(
'SELECT * FROM todo'
)
.then((rows: RowDataPacket[]) => {
res.json(rows[0]);
})
.catch((error: QueryError) => {
throw error;
})
});
我收到以下错误。
Argument of type '(rows: RowDataPacket[]) => void' is not assignable to parameter of type '(value: [RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]) => void | PromiseLike<...>'.
Types of parameters 'rows' and 'value' are incompatible.
Type '[RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]' is not assignable to type 'RowDataPacket[]'.
Type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader | FieldPacket[]' is not assignable to type 'RowDataPacket'.
Type 'RowDataPacket[]' is not assignable to type 'RowDataPacket'.
The types of 'constructor.name' are incompatible between these types.
Type 'string' is not assignable to type '"RowDataPacket"'.
为什么我有这个错误?
我认为使用require 或import 提供相同的功能,所以我很困惑。
额外信息
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
【问题讨论】:
标签: mysql node.js typescript express