Skip to content

TS - Generated types are a bit off. #19305

@antokhio

Description

@antokhio

Bug report

Required System information

  • Node.js version: 18.18.1
  • NPM version:
  • Strapi version: 4.17.1
  • Database: SQLite
  • Operating system: Windows
  • Is your project Javascript or Typescript: Typescript

Describe the bug

Hi, first, i'm really enjoying the generate-types feature, however would like to highlight few issues with end typings:

So, first of, the default value in database is always null:

export type SharedLink = Attribute.GetValue<Attribute.Component<'shared.link', false>>;
// Gives:
type SharedLink = {
    id: ID;
} & {} & {
    title?: string;
    href?: string;
}

// Noramllally if i do this by hand, i do it like this:
export type StrapiComponent<T = any> = {
  id?: number;
} & {
  [P in keyof T]: T[P];
};

export type SharedLink = StrapiComponent<{
  title: string | null;
  href: string | null;
}>;

export type Layout = StrapiContentType<{
  tel?: SharedLink | null;
]>;

so the issue is strings (as many other values, such as populated components) are defaults to null, and are not undefined unless explicitly removed with select or fields

this is going to introduce issues with optional initial values like:

const { myString = 'default' } = props; 
// this only works if myString is undefined but it's null

Second. Not sure if that's how content-type should work:

export type Layout = Attribute.GetValues<'api::layout.layout', Attribute.GetKeys<'api::layout.layout'>>;

Third. The mediaType:

export type SharedImage = Attribute.GetValue<Attribute.Component<'shared.image', false>>;
type SharedImage = {
    id: ID;
} & {} & {
    image?: any;
    title?: string;
}

Fourth. Currently you need to have /src/types/generated in every project you are going to use types.
This brings quite a complexity:

packages: 
   backend   << generates types
   frontend  << should get types updated
   shared << should get types updated

Code snippets

export type StrapiMedia = {
  id: number;
  name: string;
  alternativeText: string | null;
  caption: string | null;
  width: number | null;
  height: number | null;
  formats?: StrapiMediaFormats | null;
  hash: string;
  ext: string;
  mime: string;
  size: number;
  url: string;
  previewUrl: string | null;
  provider: string;
  provider_metadata: null | string;
  createdAt: string;
  updatedAt: string;
};

export type StrapiMediaFormats = {
  large?: StrapiMediaFormat;
  small?: StrapiMediaFormat;
  medium?: StrapiMediaFormat;
  thumbnail?: StrapiMediaFormat;
};

export type StrapiMediaFormat = {
  ext: string;
  url: string;
  hash: string;
  mime: string;
  name: string;
  path: string | null;
  size: number;
  width: number;
  height: number;
};

https://gist.github.com/antokhio/585b391bf034e2ce194ad32e44d34260
here is full code of strapi types i've been using before this feature.

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

flag: v4-unsupportedFor automatically closing legacy issuesissue: enhancementIssue suggesting an enhancement to an existing featuresource: typescriptSource is related to TypeScript (typings, tooling, ...)version: 4Issue is present in Strapi v4

Type

No type

Projects

Status

Fixed/Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions