Skip to content

Conversation

@Convly
Copy link
Member

@Convly Convly commented Jun 5, 2023

What does it do?

Initialize a new type system for Strapi

Initialize modules and namespaces to export core types and utilities from Strapi.
Core types include the basic building blocks used to create powerful APIs:

Updated the type generator to match the type system

  • Now generate one file per registry (components, content types, etc...)
  • todo: generate types for services, controllers, policies, and middlewares.
  • todo: generate index.d.ts files to load automatically registries augmentations

Updated the data transfer package to use the created types

Why is it needed?

In order to provide a better TS developer experience for Strapi users, we need to rely on solid foundations.
The goal of this type system is to act both as a toolbelt and a set of conventions for TypeScript adoption throughout the codebase.

While this is only the first version of this type system and while it should be treated as an experimental release (things might change regarding type definitions in the coming months), it should set a clear objective related to our ambitions with TypeScript.

How to test it?

  • cd examples/getstarted
  • yarn run strapi ts:generate-types
  • Two files have been generated at the root of getstarted: types/shared/registries/component.d.ts and types/shared/registries/contentTypes.d.ts and should contain type definition and dynamic registries augmentation
  • You can then use the different namespaces exported from strapi/strapi to build custom definition based on them.
    eg:
import type { Attribute, Common, Utils } from '@strapi/strapi';

declare function find<T extends Common.UID.ContentType, F extends Attribute.GetKeys<T>[]>(
  uid: T,
  fields: F
): Attribute.GetValues<T, Utils.Array.Values<F>>;

find('api::country.country', ['code']).code;
find('api::restaurant.restaurant', ['code']).code;
find('api::category.category', ['date', 'name']).date;

Related issue(s)/PR(s)

Let us know if this is related to any issue/pull request

Convly and others added 30 commits April 20, 2023 15:27
Co-authored-by: Simone <simone.taeggi@strapi.io>
Co-authored-by: Christian <christiancp100@gmail.com>
Co-authored-by: Bassel Kanso <basselkanso82@gmail.com>
Co-authored-by: Ben Irvin <innerdvations@users.noreply.github.com>
Co-authored-by: Simone <simone.taeggi@strapi.io>
Improve types for controllers and services
@Convly Convly changed the base branch from main to releases/4.11.1 June 12, 2023 07:34
Copy link
Contributor

@innerdvations innerdvations left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@strapi-bot
Copy link

This pull request has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/typescript-schema-generation-and-usage/29755/2

@strapi-bot
Copy link

This pull request has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/strapi-v4-3-with-typescript-support-and-media-library-folders-is-live/20701/7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: feature This PR adds a new feature source: typescript Source is related to TypeScript (typings, tooling, ...)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants