Skip to content

🚀 Feature: TypeScript .d.ts type declarations #4154

Description

@boneskull

AFAIK, it's recommended by both the TypeScript team and DefinitelyTyped (the people that publish everything under the @types scope on npm) that packages ship their own type declarations (.d.ts files). I think this is essentially for two reasons:

  1. It's tough to maintain types for a package you don't control
  2. The package owners are the best people to understand what the types are

I was on the fence about this for awhile, because I wasn't interested in trying to manually maintain declarations.

Recently, TypeScript introduced support for generation of type declarations from JavaScript sources.

I've used this feature pretty extensively (see report-toolkit) and have had good success with it. The way it works is this:

  1. Use JSDoc-style comment tags, where appropriate, which TS can understand.

    Mocha is already most of the way there, I think. Depending on the extent of the type inference, we may be able to restrict it to a subset the source code (initially, the user-facing, non-programmatic APIs), instead of needing to meticulously work through the entire codebase under lib/, but it's hard to say until we try it.

    Custom types (describing the shape of a plain object or the signature of a callback) are accomplished via @typedef. These are equivalent to TS' type keyword and subject to the same limitations.

    One place where we may get tripped up is that TS is unable to understand prototypal inheritance when not using the class keyword. So, though Test extends Runnable, we won't be able to illustrate that via the declaration--but if we focus on non-programmatic usage, this is moot.

    We need to make sure we're not breaking our API doc generation, though. I don't think this will be much of an issue, as that's mainly focused on the programmatic API. We can revisit the programmatic API at a later point.

  2. Use tsc to generate a .d.ts artifact from the sources we care about. Make it part of the build process; it doubles as a "type linter", because if something is incorrectly typed, the build will fail.
    Publish the resulting file(s) and keep them out of VCS.

Comments? Hate the idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: integrationsrelated to working with 3rd party software (e.g., babel, typescript)area: repository toolingconcerning ease of contributionarea: usabilityconcerning user experience or interfacecore-teamissues which must be handled by Mocha's core teamnice to haveenhancement proposal of low prioritytype: featureenhancement proposal

    Type

    No type

    Projects

    Status
    Up Next

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions