Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@types/mssql - cannot find connection pool type defs even though TS is looking in the right place

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.

like image 697
no_stack_dub_sack Avatar asked Mar 14 '18 00:03

no_stack_dub_sack


1 Answers

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)
  }
})()
like image 66
charmeleon Avatar answered Nov 10 '22 00:11

charmeleon