typed-ember / ember-cli-typescript

Use TypeScript in your Ember.js apps!
https://docs.ember-cli-typescript.com
MIT License
363 stars 99 forks source link

Cant find `meta` on `PromiseManyArray` #1539

Open Techn1x opened 1 year ago

Techn1x commented 1 year ago

Which package(s) does this problem pertain to?

Maybe others?

What are instructions we can follow to reproduce the issue?

  1. ~Update to @types/ember 4.0.2~ EDIT: This error appeared for me when I updated types/ember to 4.0.2, but the sandbox link below shows the issue and it's on types/ember 3.x, so not sure what's going on
  2. Perform a query, try to access meta key, typescript will say it doesn't exist
const booksResult = await this.store.query('book-library/book', bookSearchQuery)
console.log(booksResult.meta) // Property 'meta' does not exist on type 'ArrayProxy<BookLibraryBook>'
Reproduction Case

I've done my best to create a sandbox here. https://codesandbox.io/s/my-app-forked-4v11p8?file=/app/routes/index.ts

The sandbox IDE does not show redline errors, but if you open a terminal and type yarn tsc --build you'll see the error

Screenshot from 2022-11-11 13-31-57

Now about that bug. What did you expect to see?

meta property should exist on ArrayProxy and not cause a TS error

What happened instead?

Typescript says meta property does not exist

Screenshot from 2022-11-11 13-03-43 Screenshot from 2022-11-11 13-03-55

Thought I would try a different key update() that was mentioned in an issue that I think might be related? https://github.com/typed-ember/ember-cli-typescript/issues/1525 Screenshot from 2022-11-11 13-04-22

sleroq commented 1 year ago

Meta property does exist in my case, but I can't figure out how to specify type for it, without casting after query.

chriskrycho commented 11 months ago

Sorry for the long delay in responding! This is a gap/error in the definition of the ArrayProxy type on DefinitelyTyped, though ArrayProxy itself may not be the source of the issue. The Ember Data and Ember TypeScript teams are working on a plan to publish much-corrected types from the Ember Data packages directly, following the same overall approach as ember-source followed; in the meantime, you may be able to fix this by submitting a PR to DefinitelyTyped to correct the type, and Ember Data maintainers have the ability to review that.

chriskrycho commented 11 months ago

Ah, a further and more important update: the types for ArrayProxy itself are now guaranteed to be correct, because they’re published from Ember’s own source code. As such, if the field is public, it is in the types! The reason I referred to Ember Data types above is because the .meta field is for a ManyArray or PromiseManyArray, not for ArrayProxy itself. I'm going to update the issue description accordingly so others are not confused in the future.

Techn1x commented 11 months ago

Thanks for the response Chris (I just watched some of your Glint videos with Dan Freeman very informative thankyou!)

It has been some time since I opened this issue and with various updates it has changed somewhat. It seems that the record type is now AdapterPopulatedRecordArray<> which also doesn't seem to contain a meta key?

const booksResult = await this.store.query('book-library/book', bookSearchQuery)
const { groups = [] }: { groups: BookSearchResultsGroup[] } = booksResult.meta

Is there somewhere that I am supposed to add a type for the meta property here?

I am running Ember 4.12.3 and ember-data 4.12.3. If this issue goes away in Ember 5 or ember-data 5 let me know and I'm happy to continue ignoring it until we upgrade

This is in a service.ts file Screenshot 2023-09-29 at 12 23 32 pm

GerritSommer commented 2 weeks ago

+1