CodeDredd / pinia-orm

The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.
https://pinia-orm.codedredd.de/
MIT License
443 stars 38 forks source link

`.find` returning type `any` when using `useRepo` #1863

Closed Kieran-DK closed 3 months ago

Kieran-DK commented 3 months ago

Environment

Vue 3 (not Nuxt) + TypeScript

Reproduction

import { Model, Repository } from "pinia-orm";

export class MyItem extends Model {
  declare id: number;

  ...
}

export class MyItemRepo extends Repository<MyItem> {
  use = MyItem;

  ...
}
import { useRepo } from "pinia-orm";

const myItemRepo = useRepo(MyItemRepo);
const myItem = myItemRepo.find(1); // Type of `myItem` is `any`, not `MyItem | undefined`.

Describe the bug

When using useRepo to get a repository instance, when calling the find method, the type of the returned value is any, not the type of the Model as you would expect.

This may have been caused by the removal of .find from the Repository class: https://github.com/CodeDredd/pinia-orm/pull/1831/files#diff-de773c674180b12ca839cbf77f65443d6964b83ad45e8615e727e76601c7330fL340-L348

Additional context

No response

Logs

No response

CodeDredd commented 3 months ago

I can't really reproduce it. But i am gonna remove the one definition. Maybe it's solving your problem.

Kieran-DK commented 3 months ago

I can't really reproduce it. But i am gonna remove the one definition. Maybe it's solving your problem.

Hi @CodeDredd, thanks for looking into this. Unfortunately, after testing the fix locally, I'm still encountering the issue. If you build the pinia-orm package and view the file packages/pinia-orm/dist/shared/pinia-orm.<HASH>.d.ts, you'll see that the Repository class is missing the find method definition.

I've attached a video of the issue in action, whereby clicking through to the Repository class definition in VSCode shows that the find method is not present and so any is inferred by default.

Thanks for your help with this, greatly appreciated.

https://github.com/CodeDredd/pinia-orm/assets/148753049/14321772-e3f5-415d-865c-bd0038f53f80

CodeDredd commented 3 months ago

How does your tsconfig look like?

Kieran-DK commented 3 months ago

How does your tsconfig look like?

{
  "extends": "@vue/tsconfig/tsconfig.dom.json",
  "include": [
    "app/Vue/**/*"
  ],
  "exclude": [
    "node_modules"
  ],
  "compilerOptions": {
    "target": "ES6",
    "allowJs": true,
    "declaration": false,
    "moduleResolution": "node",
    "baseUrl": ".",
    "outDir": "",
    "paths": {
      "@/*": [
        "./app/Vue/*"
      ],
      "~": [
        "./app/*"
      ]
    },
    "types": [
      "vitest/globals"
    ]
  }
}
CodeDredd commented 2 months ago

@Kieran-DK ok i found it now. Now the typings should be working normal again. I incorrectly overloaded the Repository.

Kieran-DK commented 2 months ago

@Kieran-DK ok i found it now. Now the typings should be working normal again. I incorrectly overloaded the Repository.

Amazing, thanks @CodeDredd. I can confirm this fixes the issue 🙏

CodeDredd commented 2 months ago

@Kieran-DK Thanks for the support! Just saw it ❤️ 💯