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 declarations #878

Merged
merged 56 commits into from
Mar 2, 2021
Merged

Typescript declarations #878

merged 56 commits into from
Mar 2, 2021

Conversation

ptbrowne
Copy link
Contributor

@ptbrowne ptbrowne commented Feb 19, 2021

The idea here is to use the JSDoc comments that we already have in a lot of files of cozy-client (JSDoc is used to generate API docs automatically) to generate typescript declarations and to typecheck cozy-client at transpile time.

Gains:

  • More confidence for cozy-client's developer
  • DX is improved for cozy-client's users (IDE help)
Example

In:

/**
 * A good dog
 */
class Dog {
  constructor() {
    this.name = 'Hello'
  }
  /**
   * Woofs !
   *
   * @param {String} msg - What to woof
   * @return {void}
   */
  woof(msg) {
    this.name = 0 // PROBLEM Changing string to number
    console.log(msg)
  }
}

const dog = new Dog()
dog.meow() // PROBLEM Absent method
dog.woof() // PROBLEM Missing argument

Out:

/**
 * A good dog
 */
declare class Dog {
    name: string;
    /**
     * Woofs !
     *
     * @param {String} msg - What to woof
     * @return {void}
     */
    woof(msg: string): void;
}
declare const dog: Dog;

It means that before publishing cozy-client, a typecheck pass will be done.

  • We continue to use our existing babel transpiler

  • Typescript only emits declarations (those declarations are then picked up by IDEs to show contextual helps, see screenshots below)

    • These declarations are generated from JSDoc comments (See 1)
    • These declarations are picked up by my editor (SublimeText) if I
      install the Typescript plugin, see screenshots below
  • Checks that intra cozy-client, methods are called with the right parameters etc...

  • ⚠️ To be able to typecheck, I had to install redux@4 otherwise we had
    an error, I think we should have the redux update as part of another
    PR. See for the problem TS2315: Type 'Middleware' is not generic reduxjs/redux-thunk#205

  • ⚠️ I had to remove some errors (13) with @ts-ignore. I do not think it's problematic.

IDE screenshots

image

image

Documentation

These two can be useful before reading the PR:
https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html
https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#param-and-returns

BREAKING CHANGE: cozy-client now depends on redux 4

> https://github.com/reduxjs/redux/releases
> If you're a React user, this is going to be a lot like going from 15 to 16. Not a lot of user-facing changes, but some interesting improvements under the hood.
> The major changes (#1342) are around our TypeScript definitions, bundled CommonJS and ES builds, throwing if you subscribe or getState from a reducer, and a bunch of other smaller things. The full changes are listed below.
These are autogenerated from the JSDoc
@ptbrowne ptbrowne marked this pull request as draft February 19, 2021 09:47
@ptbrowne ptbrowne force-pushed the typescript branch 3 times, most recently from deba998 to 05d8672 Compare February 25, 2021 16:53
@ptbrowne
Copy link
Contributor Author

ptbrowne commented Mar 1, 2021

For the redux 3 to 4, since the breaking changes are mostly cosmetic and should not impact us (I haven't seen any error), I believe we could have redux@4 as dev dependency and support redux@3 and 4 in peer deps.

@ptbrowne ptbrowne marked this pull request as ready for review March 1, 2021 15:27
@ptbrowne ptbrowne requested a review from Crash-- March 1, 2021 15:27
Copy link
Contributor

@Crash-- Crash-- left a comment

Choose a reason for hiding this comment

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

Incredible work! Once green 👍 ;)

ptbrowne added 5 commits March 1, 2021 21:37
Since the false command was not the last command, its exit status
was not "returned" as the exit status of the whole process
jsdoc does not support & for intersection types and will never support
it so we need a plugin to convert & to |. More information at
https://github.com/chriseaton/jsdoc-plugin-intersection

Or we could completely forgo jsdoc and use typescript tooling to
generate our doc (this way, no incompatibility between jsdoc and
typescript)
@ptbrowne ptbrowne merged commit 2d32c58 into master Mar 2, 2021
@ptbrowne ptbrowne deleted the typescript branch March 2, 2021 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants