使用 node-postgres 的 TypeScript 编译问题
TypeScript compile problems with node-postgres
事先免责声明,我对 TypeScript 还很陌生,所以这可能是个愚蠢的问题!
我正在尝试对我的 Express/Postgres 应用程序使用相同的设置,如 node-postgres docs 中所述,其中我有一个连接到 PostgreSQL 服务器的模块,并且包含在我需要的任何地方访问它,但我在使用 TypeScript 的类型时遇到了一些问题。
在这个例子中,我简化了所有内容,完全删除了 Express。如果我这样做,一切正常,TypeScript 的编译器会很高兴:
import { Pool } from 'pg';
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT),
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
(async function getRows() {
const result = await pool.query('SELECT id, message, created FROM media');
interface dbObject {
id: number,
message: string,
created: Date
}
await result.rows.map((row: dbObject) => {
console.log(row.id);
console.log(row.message);
console.log(row.created);
})
})()
但是,如果我将 pg
函数移动到它自己单独的 db.ts
模块中:
import { Pool } from 'pg';
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT),
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
export = {
query: (text, params) => pool.query(text, params),
}
并将其导入主 app.ts
文件:
import database from './db';
(async function getRows() {
const result = await database.query('SELECT id, message, created FROM media', []);
interface dbObject {
id: number,
message: string,
created: Date
}
await result.rows.map((row: dbObject) => {
console.log(row.id);
console.log(row.message);
console.log(row.created);
})
})()
我收到了几个投诉:
src/app.ts:11:27 - error TS2345: Argument of type '(row: dbObject) => void' is not assignable to parameter of type '(value: any[], index: number, array: any[][]) => void'.
Types of parameters 'row' and 'value' are incompatible.
Type 'any[]' is missing the following properties from type 'dbObject': id, message, created
11 await result.rows.map((row: dbObject) => {
~~~~~~~~~~~~~~~~~~~~
Found 1 error.
我意识到这可能是因为我没有向我的 db.ts
文件中的 query
函数添加任何类型,但我实际上也不知道如何正确添加它们来制作这一切都有效!
好吧,这似乎行得通!我只是把db.ts
改为直接导出pool
:
import { Pool } from 'pg';
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT),
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
export = pool;
事先免责声明,我对 TypeScript 还很陌生,所以这可能是个愚蠢的问题!
我正在尝试对我的 Express/Postgres 应用程序使用相同的设置,如 node-postgres docs 中所述,其中我有一个连接到 PostgreSQL 服务器的模块,并且包含在我需要的任何地方访问它,但我在使用 TypeScript 的类型时遇到了一些问题。
在这个例子中,我简化了所有内容,完全删除了 Express。如果我这样做,一切正常,TypeScript 的编译器会很高兴:
import { Pool } from 'pg';
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT),
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
(async function getRows() {
const result = await pool.query('SELECT id, message, created FROM media');
interface dbObject {
id: number,
message: string,
created: Date
}
await result.rows.map((row: dbObject) => {
console.log(row.id);
console.log(row.message);
console.log(row.created);
})
})()
但是,如果我将 pg
函数移动到它自己单独的 db.ts
模块中:
import { Pool } from 'pg';
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT),
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
export = {
query: (text, params) => pool.query(text, params),
}
并将其导入主 app.ts
文件:
import database from './db';
(async function getRows() {
const result = await database.query('SELECT id, message, created FROM media', []);
interface dbObject {
id: number,
message: string,
created: Date
}
await result.rows.map((row: dbObject) => {
console.log(row.id);
console.log(row.message);
console.log(row.created);
})
})()
我收到了几个投诉:
src/app.ts:11:27 - error TS2345: Argument of type '(row: dbObject) => void' is not assignable to parameter of type '(value: any[], index: number, array: any[][]) => void'.
Types of parameters 'row' and 'value' are incompatible.
Type 'any[]' is missing the following properties from type 'dbObject': id, message, created
11 await result.rows.map((row: dbObject) => {
~~~~~~~~~~~~~~~~~~~~
Found 1 error.
我意识到这可能是因为我没有向我的 db.ts
文件中的 query
函数添加任何类型,但我实际上也不知道如何正确添加它们来制作这一切都有效!
好吧,这似乎行得通!我只是把db.ts
改为直接导出pool
:
import { Pool } from 'pg';
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT),
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
export = pool;