我使用的是最新版本的 mssql
包和 @types/mssql
包,我的 typeRoots 目录似乎设置正确,但我不断得到这些尝试编译 TypeScript 代码时出错:
error TS2339: Property 'connect' does not exist on type 'typeof "c:/Users/blahblahblah/node_modules/@types/mssql/index"'.
error TS2339: Property 'query' does not exist on type 'typeof "C:/Users/blahblahblah/node_modules/@types/mssql/index"'
tsconfig 似乎没问题,所以不确定发生了什么。有什么想法吗?
我总是可以只删除 @types 库,但尽量保持这里的最佳实践。
提前致谢。
编辑:这是一些示例代码,它只是一个简单的 SQL Server 连接示例:
import * as sql from 'mssql'
// test connection to SQL server using node-mssql
const connectToSqlServer = (async (): Promise<void> => {
try {
const pool = await sql.connect(config)
const result = await sql.query`select * from mock_people`
console.dir(result)
} catch (err) {
console.error(err)
}
})()
connect
和 query
下都有波浪线。当我将鼠标悬停在 sql
上时,它只显示了 import sql
而不是类型,但是正如您从错误中看到的那样,TypeScript 正在寻找正确的位置输入定义。
最佳答案
您正在尝试直接使用 sql.connect
,但看起来您应该使用 sql.ConnectionPool.connect
:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mssql/index.d.ts#L193
所以你的代码应该看起来更像这样(我没有方便的 mssql
连接所以这是未经测试的):
const connectToSqlServer = (async (): Promise<void> => {
try {
const pool = new sql.ConnectionPool(config);
pool.connect().then(() => {
const request = new sql.Request(pool);
const result = await request.query(`select * from mock_people`);
console.dir(result);
});
} catch (err) {
console.error(err)
}
})()
关于typescript - @types/mssql - 即使 TS 在正确的位置查找,也找不到连接池类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49267785/