I am using the latest versions of the mssql
package and the @types/mssql
package, and my typeRoots directory seems to be correctly set, but I keep getting these errors when trying to compile the TypeScript code:
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"'
The tsconfig seems to be ok, so not sure what's going on. Any ideas?
I can always just remove the @types library, but trying to keep with best practices here.
Thanks in advance.
EDIT: Here is some sample code, it's just a straightforward SQL Server connection example:
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)
}
})()
both connect
and query
have squiggles under them. When I hover over sql
, it just shows me the import sql
rather than the type, but as you can see from the error, TypeScript is looking in the right place for the type def.
You're trying to use sql.connect
directly but it looks like you should be using sql.ConnectionPool.connect
: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mssql/index.d.ts#L193
So your code should look more like this (I don't have a mssql
connection handy so this is untested):
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)
}
})()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With