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:
- It's tough to maintain types for a package you don't control
- 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:
-
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.
-
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?
AFAIK, it's recommended by both the TypeScript team and DefinitelyTyped (the people that publish everything under the
@typesscope on npm) that packages ship their own type declarations (.d.tsfiles). I think this is essentially for two reasons: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:
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'typekeyword 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
classkeyword. So, thoughTestextendsRunnable, 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.
Use
tscto generate a.d.tsartifact 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?