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

typeCast Next type appears to be incorrect #3122

Open
bbugh opened this issue Oct 15, 2024 · 3 comments · May be fixed by #3129
Open

typeCast Next type appears to be incorrect #3122

bbugh opened this issue Oct 15, 2024 · 3 comments · May be fixed by #3129

Comments

@bbugh
Copy link

bbugh commented Oct 15, 2024

Problem

The current return type of Next is void. This led to a bug in the code because as currently typed, the return value of next appears to be unnecessary.

export type Next = () => void;
export type TypeCast = ((field: Field, next: Next) => any) | boolean;

I discovered in my code that unless next() was returned, this function would result in many undefined values. However, in every case that I can find it, next is called as return next(). If it is not returned, the result is empty data.

// bad, results in all `undefined` values:
typeCast: (field, next) => { next() }

// good, returns expected results
typeCast: (field, next) => { return next() }

Additionally, typescript-eslint correctly raises an error on this with the @typescript-eslint/no-confusing-void-expression rule:

Example 1
Pasted_Image_10_15_24__1_49 PM

Example 2
image

Proposed solution

Since the return value is required, but the type is not known, the unknown type seems like the most correct option here. I tested and this resolves the issue.

mysql2/typings/mysql/lib/parsers/typeCast.d.ts
-export type Next = () => void;
+export type Next = () => unknown;

I can submit a PR if requested (and it will be accepted).

@bbugh bbugh changed the title TypeCast and Next types appear to be incorrect typeCast Next type appears to be incorrect Oct 15, 2024
@wellwelwel
Copy link
Collaborator

// good, returns expected results
typeCast: (field, next) => { return next() }

@bbugh, could you help with a basic debug (if you can't, I'll try to check it out later)?

What would console.log(next()) return in general or when would it return something other than void | undefined?

@bbugh
Copy link
Author

bbugh commented Oct 16, 2024

console.log(next()) will print whatever the value of the field is. I don't think it should ever be void:

"a3adef49-a125-5e83-bc84-0a9f57f5b167"
"5b455423-7961-53b7-a627-93db69ab295c"
"ac4233ad-b60e-5ce9-9185-3d366815348c"
4

Here is further demonstration of the issue. The console output of the following code when typeCast next return value is not or is returned:

const [rows] = await connection.query(sql, values);
console.log(rows);

❌ Without returning next, no fields have values

typeCast: (field, next) => { next(); }, // notice no "return"
{
  id: undefined,
  projectId: undefined,
  taskId: undefined,
  status: undefined,
},
{
  id: undefined,
  projectId: undefined,
  taskId: undefined,
  status: undefined,
}

✅ With returning next, it returns the field values

typeCast: (field, next) => { return next() },
{
  id: "a3adef49-a125-5e83-bc84-0a9f57f5b167",
  projectId: "5b455423-7961-53b7-a627-93db69ab295c",
  taskId: "ac4233ad-b60e-5ce9-9185-3d366815348c",
  status: 4,
},
{
  id: "cf483a80-52a0-5f53-8b24-03dc9d6dc60c",
  projectId: "01b821bf-e4a8-5953-8d59-72d1a28b4543",
  taskId: "2a803da9-b6c6-5746-989b-ca07832d0a72",
  status: 2,
}

@wellwelwel
Copy link
Collaborator

Thanks for the debug, @bbugh 🤝

I can submit a PR if requested (and it will be accepted).

Please, feel free to contribute 🚀

bbugh added a commit to bbugh/node-mysql2 that referenced this issue Oct 17, 2024
Fixes typeCast Next type appears to be incorrect sidorares#3122
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants