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

error TS2536: Type 'string' cannot be used to index type 'T' #20

Closed
BryanWilhite opened this issue Jan 7, 2020 · 7 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@BryanWilhite
Copy link
Owner

BryanWilhite commented Jan 7, 2020

> tsc

src/utilities/array.utility.ts:48:41 - error TS2536: Type 'string' cannot be used to index type 'T'.

48             ) => ((accumulator[key] || (accumulator[key] = [])).push(current), accumulator),
                                           ~~~~~~~~~~~~~~~~

src/utilities/array.utility.ts:73:41 - error TS2536: Type 'number' cannot be used to index type 'T'.

73             ) => ((accumulator[key] || (accumulator[key] = [])).push(current), accumulator),
                                           ~~~~~~~~~~~~~~~~


Found 2 errors.

there are only 5 StackOverflow articles related to TS2536: https://stackoverflow.com/search?q=ts%282536%29

these @mariusschulz articles might be of help:

@BryanWilhite BryanWilhite added the bug Something isn't working label Jan 7, 2020
@BryanWilhite BryanWilhite self-assigned this Jan 7, 2020
@BryanWilhite
Copy link
Owner Author

This is new as of 3.5.1, there was no error in 3.4.5.

microsoft/TypeScript#31661

@BryanWilhite
Copy link
Owner Author

BryanWilhite commented Jan 7, 2020

Given a type variable T with a constraint C, when an indexed access T[K] occurs on the target side of a type relationship, index signatures in C are now ignored. This is because a type argument for T isn't actually required to have an index signature, it is just required to have properties with matching types.

microsoft/TypeScript#32704 (comment)

so, i have written grouping utilities based on generics, T extends { [key: string]: any }, and i think what the above is saying is that key stuff extending T is now ignored Typescript 3.5.x

@BryanWilhite
Copy link
Owner Author

BryanWilhite commented Jan 7, 2020

this approach might be an alternative: https://gist.github.com/Mds92/053deeba7349ce59038d9980c5dba612

my gist shows how simple things can be by just using any and Function: https://stackblitz.com/edit/mds92-group-by?file=index.ts

not sure what version of Typescript is in use on StackBlitz

BryanWilhite pushed a commit that referenced this issue Jan 8, 2020
@BryanWilhite
Copy link
Owner Author

i think a solution is in place as tests ☔ are failing 🔥

ok, let me attempt to explain to my future self what has been done:

  • ArrayUtility.groupBy replaces the previous two grouping functions
  • ArrayUtility.groupBy returns an array of ReducedGroup, a new type to generalize grouping
  • ReducedGroupUtility.reduceToObject<T> was added to transform an array of ReducedGroup to an object of the form, say, used by DisplayItemUtility.group

the tests for DisplayItemUtility.group are of course failing 🔬

@BryanWilhite
Copy link
Owner Author

it looks like ReducedGroupUtility.reduceToObject<T> is returning this unexpected shape:

console.log src/utilities/display-item.utility.ts:117
    group { reduced: { key: 'group-one', values: [ [Object], [Object] ] } }

BryanWilhite pushed a commit that referenced this issue Jan 8, 2020
@BryanWilhite
Copy link
Owner Author

more tests are passing:

console.log src/utilities/reduced-group-utility.ts:32
    { reduction: 
       { 'group-three': [ [Object] ],
         'group-two': [ [Object], [Object], [Object], [Object] ],
         'group-one': [ [Object], [Object] ] } }

BryanWilhite pushed a commit that referenced this issue Jan 8, 2020
@BryanWilhite
Copy link
Owner Author

my apologies to anyone out there getting a breaking change in a minor version: https://www.npmjs.com/package/songhay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant