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

Typescript definitions support #3994

Open
SimonChaumet opened this issue Jan 11, 2023 · 3 comments
Open

Typescript definitions support #3994

SimonChaumet opened this issue Jan 11, 2023 · 3 comments

Comments

@SimonChaumet
Copy link

The problem to solve

When developing modules based on the self pattern we often don't see if a function really exists, where it is declared etc.

Proposed solution

Add typescript definition file at root project and

Alternatives

  • rewrite apostrophe in typescript or other typed language that transpiles to javascript. This option would cost a lot with few benefits
  • use jsdoc. This option is complicated because jsdoc does not handle all the type, however we can combine jsdoc and typescript definitions together.

Additional context

I already started a POC with few definitions

@boutell
Copy link
Member

boutell commented Jan 11, 2023

Hi Simon, happy to have this conversation! Can you shed any light on how this would handle a situation where a project-level module extends a core module like @apostrophecms/piece-type? As far as I know there would be no obvious way for the typings to be understood as relevant to that situation.

@boutell
Copy link
Member

boutell commented Jan 11, 2023

My thinking is that if you don't get type-checking of inherited methods, it's not accomplishing a lot.

We do have long-term internal plans to try to figure out a fully effective strategy for typescript and Apostrophe but outside input could be very helpful in choosing that strategy.

@SimonChaumet
Copy link
Author

SimonChaumet commented Jan 12, 2023

I've added the pull request with the draft code

To add module definitions you just need to override the interfaces on module side (in a typescript definition file) :

declare module "apostrophe" {
  interface ApostropheModulesOptions {
    "@apostrophecms/piece-type": {
      someProperty: number
    }
  }

  interface ApostropheModulesApi {
    "@apostrophecms/piece-type": {
      someMethodOnSelf(): void
    }
  }
}

on module side you will need to add the method createModule to allow type inference

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

No branches or pull requests

2 participants