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

Nested optional arrays are not callable #53

Open
frdwhite24 opened this issue Sep 4, 2023 · 4 comments
Open

Nested optional arrays are not callable #53

frdwhite24 opened this issue Sep 4, 2023 · 4 comments

Comments

@frdwhite24
Copy link

Description:
Optional nested arrays are not callable after being assigned Camelize type with appropriate generic.

Steps to Reproduce:

  1. Import the Camelize type from 'camelize-ts'.
  2. Declare a variable bar of type Camelize<{ foo?: string[] }[]> and initialize it with an array containing an object with an optional foo property.
  3. Attempt to use the map method on baz (the nested array items).
import { Camelize } from 'camelize-ts'

const bar: Camelize<{ foo?: string[] }[]> = [
  {
    foo: ['abc'],
  },
]

bar.map((baz) => baz.foo?.map((x) => x))
// 1. This expression is not callable. (<- it's referring to baz.foo.map not the bar.map call.)
//     Type 'CamelizeObject<(<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]), false>' has no call signatures. [2349]

Expected Behavior:
The map method should work as expected, allowing access to the foo property and its elements (if I remove the optional marker in the generic then things do work as expected i.e. Camelize<{ foo:string[] }[]>)

Actual Behavior:
The TypeScript compiler raises an error, stating: "This expression is not callable. Type 'CamelizeObject<((callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]), false>' has no call signatures."

Additional Information:

  • Library Version: 3.0.0
  • TypeScript Version: 5.2.2

Note:
This issue affects the usability of the library when working with optional nested arrays, and a solution or workaround would be greatly appreciated.

@frdwhite24
Copy link
Author

If anyone is able to direct me to the right part of the codebase, i'd be happy to try to get a PR up to address this issue, if you agree it is an issue 😊

@megachomba
Copy link

helllo, comfiriming having the same issue here.... this needs to be fixed. as a workaround im expliictly making type assertion of
bar.map((baz) => baz.(foo as string[])?.map((x) => x))
but its disgusting to read in the codebase

@kbrabrand
Copy link
Owner

👋 I have no objections here and agree that it's an issue that should be fixed. I've been swamped in work lately and haven't had a chance to follow up. Sorry.

As for where in the code it's located: https://github.com/kbrabrand/camelize-ts/blob/main/src/index.ts#L10-L12

@frdwhite24
Copy link
Author

hi @kbrabrand thanks for the response.

I wasn't able to get the time at work to dig into this unfortunately, and we came across https://github.com/gustavoguichard/string-ts which has a much cleaner inferred type as the response and also didn't suffer from this bug.

For now we've switched over, but thanks for the hard work on this library and good luck with it

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

3 participants