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

Tracking Issue: Typescript Type generation #15225

Open
derrickmehaffy opened this issue Dec 20, 2022 · 7 comments
Open

Tracking Issue: Typescript Type generation #15225

derrickmehaffy opened this issue Dec 20, 2022 · 7 comments
Labels
flag: tracking issue Special issues used to combine multiple issues into one issue: bug Issue reporting a bug issue: enhancement Issue suggesting an enhancement to an existing feature severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: confirmed Confirmed by a Strapi Team member or multiple community members

Comments

@derrickmehaffy
Copy link
Member

derrickmehaffy commented Dec 20, 2022

Bug report

Describe the bug

This is a tracking issue for all issues related to Typescript type generation issues (bugs, requests, enhancements, etc)

List of initial issues that contributed to creating this tracking issue:

Steps to reproduce the behavior

See various linked issues

Expected behavior

Various type issues and requests should be addressed at a later date

Additional context

Additional duplicate issues will be linked in the comments below:

@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug issue: enhancement Issue suggesting an enhancement to an existing feature severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members flag: tracking issue Special issues used to combine multiple issues into one source: typescript Source is related to TypeScript (typings, tooling, ...) labels Dec 20, 2022
@derrickmehaffy derrickmehaffy pinned this issue Dec 20, 2022
@christiancp100 christiancp100 unpinned this issue Jan 3, 2023
@sjoukedv
Copy link
Contributor

+1 on this one!

I generate the typescript definitions from Strapi and import it in our NextJS frontend. The id field of the REST API response is not being used for e.g. ApiCollectionCollection type (the relation attributes format is also off):

{
  data: {
    id: 1 // <-- this id is missing from the definition
    attributes: {
      ...
      relationField: { // <-- e.g. has type MediaAttribute, but needs to be wrapped in StrapiResponse
        id: ...,
        attributes: {
           // field attributes
        }
   }
  }
}

Where my workaround is to define another type StrapiResponse<ApiCollectionCollection> and use

export interface StrapiResponse<T> {
  id: number
  attributes: T['attributes']
}

I think the relational attribute should also be wrapped in such a 'StrapiResponse' to enfore the type is equal.
Perhaps this issue is specific to REST and works better with something like GraphQL, but in that case it's still valueable to have the exact type definition (maybe with REST prefix?).

@julesrenaud
Copy link

@sjoukedv look into this package, I installed it as a global package and had pretty good results with it https://www.npmjs.com/package/strapi-generate-types

@axelinternet
Copy link

Do you accept PRs for the sub issues?

Something like this that is crashing the whole application for me because of the invalid types is something that seems like a pretty quick fix:

interface FindParams<T> {
select?: (keyof T)[];
// TODO: add nested operators & relations
where?: WhereParams<T>;
limit?: number;
offset?: number;
orderBy?: // TODO: add relations
| Sortables<T>
| Sortables<T>[]
| { [K in Sortables<T>]?: Direction }
| { [K in Sortables<T>]?: Direction }[];
// TODO: define nested obj
populate?: (keyof T)[];
}

@arpadgabor
Copy link

Hello, are there any workarounds or plans to fix these type issues? I know it may not be trivial but it's a rather big issue when trying to build reliable apps. I might be open to try and fix some issues in a PR if there's any follow-up.

@simohr
Copy link

simohr commented Aug 7, 2023

Hello, are there any workarounds or plans to fix these type issues? I know it may not be trivial but it's a rather big issue when trying to build reliable apps. I might be open to try and fix some issues in a PR if there's any follow-up.

Only quick thing I was able to do is add // @ts-ignore above the line where you get the error.

@gregg-cbs
Copy link

Looks like a lot of tickets were closed but not merged as this is still an issue?

@Boegie19
Copy link
Collaborator

@gregg-cbs Correct they where all closed in and this 1 tracking issue opened in its place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: tracking issue Special issues used to combine multiple issues into one issue: bug Issue reporting a bug issue: enhancement Issue suggesting an enhancement to an existing feature severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: confirmed Confirmed by a Strapi Team member or multiple community members
Projects
Status: To be reviewed (Open)
Status: To Review
Status: No status
Status: Reproducible on v4
Development

No branches or pull requests

8 participants