If you know how to fix the issue, make a pull request instead.
@types/mssql package and had problems.Definitions by: in index.d.ts) so they can respond.If you do not mention the authors the issue will be ignored.
@types/mssql is not up to date with the latest version of mssql as Promise-based functions such as mssql.connect and mssql.query are highlighted as invalid with @types/mssql installed.
Please have a look at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mssql/index.d.ts. The promise based API is supported. Could you please elaborate on which specific functions you are missing, including a reference to the original documentation?
Hi @pkeuter, thanks for the reply. I've checked the mssql declaration file, but I'm not too sure how to interpret it.
I'm using Quick Example from the MSSQL Github README. However, I get errors (stated below) from Visual Studio Code.
import * as sql from 'mssql';
async () => {
try {
const value = 1;
await sql.connect('mssql://username:password@localhost/database')
const result = await sql.query(`select * from mytable where id = ${value}`)
console.dir(result)
} catch (err) {
// ... error checks
}
}

await sql.connect('mssql://username:password@localhost/database')
[ts] Property 'connect' does not exist on type 'typeof import("[REDACTED]/node_modules/@types/mssql/index")'. [2339]
const result = await sql.query(`select * from mytable where id = ${value}`)
[ts] Property 'query' does not exist on type 'typeof import("[REDACTED]/node_modules/@types/mssql/index")'. [2339]
any
"dependencies": {
"express": "^4.16.4",
"mssql": "^4.3.0",
"tedious": "^4.1.3"
},
"devDependencies": {
"@types/del": "^3.0.1",
"@types/express": "^4.16.0",
"@types/gulp": "^4.0.5",
"@types/gulp-uglify": "^3.0.6",
"@types/mssql": "^4.0.11",
"@types/node": "^10.12.18",
"@types/pump": "^1.0.1",
"@types/tedious": "^2.6.1",
"del": "^3.0.0",
"gulp": "^4.0.0",
"gulp-typescript": "^5.0.0",
"gulp-uglify": "^3.0.1",
"pump": "^3.0.0",
"ts-node": "^7.0.1",
"typescript": "^3.2.4"
}
`
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"target": "es2015",
"lib": ["es2015"]
}
}
@achrinza I just came across this myself. Having a look at your code, I think you need to remove the parentheses, or TS will convert it to a string and not pass it through as a templated string
import * as sql from 'mssql';
async () => {
try {
const value = 1;
await sql.connect('mssql://username:password@localhost/database')
const result = await sql.query`select * from mytable where id = ${value}`
console.dir(result)
} catch (err) {
// ... error checks
}
}
Having read the mssql code, I think the following APIs can be modified
export declare class Request extends events.EventEmitter {
public query(command: string): Promise<IResult<any>>;
public query(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
public query<Entity>(command: string): Promise<IResult<Entity>>;
public query<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
public batch(batch: string): Promise<IResult<any>>;
public batch(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
public batch<Entity>(batch: string): Promise<IResult<Entity>>;
public batch<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
public batch<Entity>(batch: string, callback: (err?: any, recordset?: IResult<Entity>) => void): void;
}
export declare class ConnectionPool extends events.EventEmitter {
public query(command: string): Promise<IResult<any>>;
public query(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
public query<Entity>(command: string): Promise<IResult<Entity>>;
public query<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
public batch(batch: string): Promise<IResult<any>>;
public batch(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
public batch<Entity>(batch: string): Promise<IResult<Entity>>;
public batch<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
}
Thanks for the help @pleb !
Is there a reason for this Typescript behavior with template strings? I haven't come across documentation explaining it.
Does this commit resolve the "'connect' does not exist on type..." issue? Not too sure how changing the accepted types for mssql.Request would fix the issue
@achrinza I've found this online book to be pretty amazing. I would suggest giving it a quick read. https://basarat.gitbooks.io/typescript/docs/template-strings.html
@achrinza, I think
Thanks for the help @pleb !
Is there a reason for this Typescript behavior with template strings? I haven't come across documentation explaining it.
Does this commit resolve the "'connect' does not exist on type..." issue? Not too sure how changing the accepted types for mssql.Request would fix the issue
@achrinza, it does not. It also does not expose query globally. If mssql supports this, you might want to send in a PR to expose these methods. You can also use the slightly more verbose (but better imho) method, which looks a bit like this:
import * as sql from 'mssql';
async () => {
try {
const pool = new sql.ConnectionPool({
server: 'localhost',
user: 'username',
password: 'password',
database: 'database'
});
const value = 1;
await pool.connect()
const result = await pool.query(`select * from mytable where id = ${value}`)
console.dir(result)
} catch (err) {
// ... error checks
}
}
Good luck!
@achrinza I just came across this myself. Having a look at your code, I think you need to remove the parentheses, or TS will convert it to a string and not pass it through as a templated string
@pleb, this is true. But not the issue @achrinza is having. It just means that there won't be any sanitizing because the string will be passed parsed to mssql instead of letting mssql parsing the template string.
So
pool.query`select * from mytable where id = ${value}`;
is sanitized and thus better than
pool.query(`select * from mytable where id = ${value}`);
But they should work both!
PR didn't make it. 馃槥
[email protected] released, types are still at 4.3.1
Hi @pkeuter, thanks for the reply. I've checked the mssql declaration file, but I'm not too sure how to interpret it.
I'm using Quick Example from the MSSQL Github README. However, I get errors (stated below) from Visual Studio Code.
Code:
import * as sql from 'mssql'; async () => { try { const value = 1; await sql.connect('mssql://username:password@localhost/database') const result = await sql.query(`select * from mytable where id = ${value}`) console.dir(result) } catch (err) { // ... error checks } }Screenshot:
Errors
Code
await sql.connect('mssql://username:password@localhost/database')Error
[ts] Property 'connect' does not exist on type 'typeof import("[REDACTED]/node_modules/@types/mssql/index")'. [2339]Code
const result = await sql.query(`select * from mytable where id = ${value}`)Error
[ts] Property 'query' does not exist on type 'typeof import("[REDACTED]/node_modules/@types/mssql/index")'. [2339] anyDependencies
"dependencies": { "express": "^4.16.4", "mssql": "^4.3.0", "tedious": "^4.1.3" }, "devDependencies": { "@types/del": "^3.0.1", "@types/express": "^4.16.0", "@types/gulp": "^4.0.5", "@types/gulp-uglify": "^3.0.6", "@types/mssql": "^4.0.11", "@types/node": "^10.12.18", "@types/pump": "^1.0.1", "@types/tedious": "^2.6.1", "del": "^3.0.0", "gulp": "^4.0.0", "gulp-typescript": "^5.0.0", "gulp-uglify": "^3.0.1", "pump": "^3.0.0", "ts-node": "^7.0.1", "typescript": "^3.2.4" }tsconfig.json
{ "compilerOptions": { "module": "commonjs", "noImplicitAny": false, "target": "es2015", "lib": ["es2015"] } }```
That still isn't working.
@achrinza I just came across this myself. Having a look at your code, I think you need to remove the parentheses, or TS will convert it to a string and not pass it through as a templated string
@pleb, this is true. But not the issue @achrinza is having. It just means that there won't be any sanitizing because the string will be passed parsed to mssql instead of letting mssql parsing the template string.
So
pool.query`select * from mytable where id = ${value}`;is sanitized and thus better than
pool.query(`select * from mytable where id = ${value}`);But they should work both!
^ @focux Have you tried the suggested solution? This has worked without issues for me.
@achrinza I believe your second example leaves you open to sql injection attacks
@pleb Apologies, I was referring to the example using tagged templates (which should sanitize the SQL query).
No, it does not work. The problem is that when you try to do this:
const pool = await sql.connect(config);
You get this error:
[ts] Property 'connect' does not exist
I had to add the types to my project and fixed the types locally. I will open a PR soon with the fixes that have to be made in order for it to work.
Was this ever resolved in the types? I'm seeing this with @types/mssql": "6.0.4
No, unfortunately somebody didn鈥檛 understand the syntax and the PR got rejected
@pleb do you have gist or another supplemental/override that you use to get around this that you could share?
Sorry, no gist on hand. I think for the project where I came across this, I defined a local type with the template syntax