-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
🚀 Feature: TypeScript .d.ts type declarations #4154
Comments
I'm super happy to see the enthusiasm here! I'd be a bit cautious though because depending on how your types differ, users may see some differences (at least initially) or have dependencies on how the types are declared. I think it's still worth trying to compare/contrast though! I'd also heavily recommend using |
(also we're looking for feedback!) |
You'd probably wanna try to make sure your API that's generated is compatible with the one exposed by |
@weswigham Meaning that if someone has |
Yep. A design decision made a long time ago was that a package's own declarations take priority over declarations from the |
I had a look at DefinitelyTyped - Mocha, it supports Mocha v5.2 and takes about 3000 lines. We should start improving our JSDoc for that purpose, but not ship yet any If we start shipping @types, the quality has to be excellent, otherwise we get flooded by issues we are not able to handle. @cspotcode was involved in |
We should also be able to sprinkle type-only We can use the DefinitelyTyped tests to confirm compatibility. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mocha/mocha-tests.ts Can mocha start shipping its own experimental .d.ts in an opt-in, non-automatic fashion? Consumers would choose to use mocha's bundled declaration if they want, but it would not override
The Mocha team should have a list of people interested in bugfixing the declarations. Whenever declaration bugs are filed, those people can be pinged and asked to write a PR. I can be on that list to start. Is mocha's release process automated? If it's straightforward to publish patch releases, this'll be much easier. |
How would you ship “opt-in” types? |
I don’t want to manually maintain a declaration file either. That’s the idea: it’s generation would be automated from our jsdoc docstrings, of which we have quite a bit already. but it’s also why this is labeled |
I haven't verified that this works, but I have an idea. Maybe the TypeScript team knows if it's do-able. We could ship a If a mocha user wants to opt-in to our bundled types, they have to manually refer to that d.ts file via their tsconfig or a EDIT: to clarify, during this experimental phase, I imagine the types would only be used by adventurous users who know how to configure opt-in and how to help us diagnose any bugs. The goal is to improve the types enough that they can eventually be automatic, and the DefinitelyTyped declarations can be deprecated. |
I switched on checkJs and it's already finding bugs / code smells. It's cool to see this pay off in other ways. For example, https://github.com/mochajs/mocha/blob/master/lib/cli/run-helpers.js#L85 |
I'm at a stopping point with #4156 for the moment. It builds the declarations and tests them against |
Has anyone had a chance to look at this? Do you have any questions? |
Would you consider accepting a PR converting mocha's own code to (strict) native typescript? A PR that would typecheck mocha's own code, generate .d.ts from sources, and ensure compatibility with @types/mocha. |
Coming back to this a few years later: the general TypeScript community consensus is now that package should only ship their own types if they're able to generate them automatically. As much as I love TypeScript, per #5027 we're not looking to do any major changes like switching the source code language any time soon. This issue is blocked on us:
|
Note that global declarations are not auto-included for non As such, if Mocha shipped its own types and you as a consumer wanted all its global declarations included in your project, you'd have to now add something like {
"compilerOptions": {
"types": ["mocha"]
}
} That might be considered a best practice, but it's going to break a lot of existing projects relying on automatic types inclusion. |
I personally had the integration of the types of mocha into mocha itself on my agenda, as we do it in fastify. I am not aware of a consensus, that only projects, which generate the typescript types by itself should ship them with the project. @JoshuaKGoldberg Do you have some links regarding this. @DanielRosenwasser |
From https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html:
|
Just noting for visibility: we're still not ready to take this on. #4228 will have to come first to clean up the internal JSDocs. So if anybody wants to drive this forward, that'd be a good issue to start with! We also have several other things to get through before we can really give this issue a lot of attention:
|
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: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, thoughTest
extendsRunnable
, 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?
The text was updated successfully, but these errors were encountered: