Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot properly type results for query with rowsAsArray option set to true #55

Open
teklakct opened this issue Dec 8, 2022 · 0 comments

Comments

@teklakct
Copy link

teklakct commented Dec 8, 2022

Hi

I don't know what I'm doing wrong. I cannot properly type results when the option rowsAsArray is enabled.
My SQL returns an array of arrays that contains only one string eg:

// connection.promise().query("SHOW TABLES LIKE 'table_%';");
[
  [ 'table_1'],   [ 'table_2'],
  [ 'table_3'],   [ 'table_4'], 
]

I want to flat this result in my helper function but TS complaints about types mismatch.
See example below

const listTablesSql = "SHOW TABLES LIKE 'table_%';"

async strictTypeInQuery(): Promise<string[]> {
        // error: `Type 'string' is not assignable to type 'RowDataPacket'`
        const [rows] = await connection.promise().query<string[]>({sql: listTablesSql, rowsAsArray: true}); 

        return rows.flat(1);
}

async inferType(): Promise<string[]> {
        // error: `Type 'string' is not assignable to type 'RowDataPacket'`
        const [rows] = await connection.promise().query({sql: listTablesSql, rowsAsArray: true}); 

        return rows.flat(1); // TS:error `Type 'RowDataPacket' is not assignable to type 'string'.`
}

Can you help me and point what I'm doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant