-
-
Notifications
You must be signed in to change notification settings - Fork 699
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
Copied typings from DefinitelyTyped #1092
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1092 +/- ##
=======================================
Coverage 93.61% 93.61%
=======================================
Files 32 32
Lines 1628 1628
Branches 393 393
=======================================
Hits 1524 1524
Misses 104 104 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this looks like a great start on this, and that the tests are passing, this seems like a good opportunity to merge this in and iterate from here.
🎉
@keithamus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good but I'd like some clarification around the use of the type reference. Packaging broken definitions is worse than no definitions.
lib/chai.d.ts
Outdated
@@ -0,0 +1,1518 @@ | |||
// <reference types="assertion-error"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does it deal with this type reference? Does it forces the user to install the types for assertion-error
itself? This may pose issues due to versions mismatch between the version actually used by chai and the one uplifted to the consumer project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we bundle an specific version of this type definition to be used with Chai? This way we could always ensure it would be right (instead of depending on peer-deps).
(I don't understand much about TypeScript, so please let me know if I misunderstood what you've just said)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link you posted refers to the types for the typings tool. This tool was used before Typescript 2, because typescript did not support types from npm. These repos are currently (slowly) maintained for older tools but the recommended way is to publish the types to the library's repository. If this is impossible, the solution is to use DefinitelyTyped. Definitions in this repo are automatically published to the @types
npm organization.
There are two solutions for the current issue:
- Adding type definitions to
assertion-error
(better) - adding
@types/assertion-error
as a normal dependency topackage.json
(quicker)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first option seems more adequate indeed.
As I've seen in the assertion-error
repo, it's not that big, so it might be a simple issue to attack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I had a note to deal with this and forgot about it...
+1 to adding type definitions to assertion-error
and -1 to adding any extra dependencies to package.json
. I'll tackle that now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, just looking through this code, it looks like AssertionError
is manually copy & pasted below. The comment above is //
instead of ///
and so doesn't do anything. I'll remove it.
Included them as lib/chai.d.ts, with --noEmit tests run in Makefile against test/typings/*.ts. [email protected] is now a dev dependency. Closes https://github.com/chaijs/chai#650 and https://github.com/types/npm-chai#14. I intentionally copy & pasted what's now used under @types/chai despite it being a little... sloppy in some parts (why the Resharper comment??). Improving the typings would be great as followup issues IMO. Non-merge-conflict version of #822.
@demurgos I've removed the reference... but looks like coverage is calculated as decreased on the PR. I don't really know how to deal with that :( |
Thanks for update. I checked the Regarding coverage, I don't see any change: https://codecov.io/gh/chaijs/chai/pull/1092 Is it the right link? (Or do you only see the reduction in local?) |
Huh, I was seeing a coveralls integration saying it was reduced. Never mind! |
I am having trouble to run the tests in your branch. Do you just use Edit: Solved, but I don't have much time right now. I'll submit a review later. |
Hi, Here are the main changes:
Chai uses strict commonJS (the exported object is just a namespace, not a function) so ideally the type definitions should avoid the |
The types for I think I'll update my branch and send a new PR superseding this one once the types are on npm. (See previous message) |
I don't have any experience with TypeScript, and don't have the time to get smart on it, so I'll defer to other contributors and maintainers on this PR. Question though: What does merging this PR mean in terms of future changes to Chai? In particular, do all future new assertions and changes to existing assertions require corresponding updates to these new TypeScript-specific files before they can be safely merged? |
Additions to the Javascript code will need to be documented in the type definitions to be usable from Typescript. It has no runtime impact on Javascript users. You have two ways to approach this:
You can also have a middle-ground were you declare methods and functions with the The reason behind the addition of the types was that formalizing the documentation as types is valuable and having it here would help to have control over it and keep it in sync. Comment that prompted the PR. I also explained my reasons in this |
What I'm most concerned about is breaking changes to existing assertions. If a breaking change is made to an existing assertion, is there an expectation that the type definitions need to be synchronized prior to the next semver major release, or is it generally understood and accepted by the community that such synchronization can happen in a patch after the major release? |
Right, I mainly addressed feature additions but not breaking changes.
These are the same trade-offs as for documentation: incomplete documentation is tolerable, wrong documentation is harmful. (That's why I call and treat it just as a more formal documentation). From a practical point of view, the goal of types is to catch some errors before running the code. Because of this, I'd say that updating the types when breaking changes occur is when it's the most important. It's a great help for migration: they become very valuable because they allow to immediately find the lines that need to be updated (or most of them). |
This commit adds type definitions for Chai. These type definitions are based on the types from DefinitelyTyped. They enable Typescript users to use the library directly and serve as documentation. The main changes from the DT version are: - Use external module style - Use `AssertionError` types from `assertion-error` - Fix global modification of objects with `should` The types can be improved in further PRs. This commit also adds tests for the type definitions. It tries to compile (in noEmit mode) the files in `test/typings` using the type definitions. This adds `typescript` as a new dev dependency. For maintenance and semver, the types should be treated as documentation. They should be kept up-to-date with the code, especially when breaking changes occur (wrong documentation is worse than incomplete documentation), see discussion in chaijs#1092. [Help for type definitions][ts-declarations] - Closes chaijs#650 (original issue) - See chaijs#822 (previous PR with merge conflicts) - Closes chaijs#1092 (superseded by this PR) [ts-declarations]: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
I updated my branch to use the new |
This commit adds type definitions for Chai. These type definitions are based on the types from DefinitelyTyped. They enable Typescript users to use the library directly and serve as documentation. The main changes from the DT version are: - Use external module style - Use `AssertionError` types from `assertion-error` - Fix global modification of objects with `should` The types can be improved in further PRs. This commit also adds tests for the type definitions. It tries to compile (in noEmit mode) the files in `test/typings` using the type definitions. This adds `typescript` as a new dev dependency. For maintenance and semver, the types should be treated as documentation. They should be kept up-to-date with the code, especially when breaking changes occur (wrong documentation is worse than incomplete documentation), see discussion in chaijs#1092. [Help for type definitions][ts-declarations] - Closes chaijs#650 (original issue) - See chaijs#822 (previous PR with merge conflicts) - Closes chaijs#1092 (superseded by this PR) [ts-declarations]: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
Included them as
lib/chai.d.ts
, with--noEmit
tests run inMakefile
againsttest/typings/*.ts
. [email protected] is now a dev dependency.Closes https://github.com/chaijs/chai#650 and https://github.com/types/npm-chai#14.
I intentionally copy & pasted what's now used under
@types/chai
despite it being a little... sloppy in some parts (why the Resharper comment??). Improving the typings would be great as followup issues IMO.Non-merge-conflict version of #822.
(note: I don't haveAppVeyor failures look unrelated. Can this get merged anyway? 😄make
locally, so I'm using AppVeyor/Travis as my build machine. Ignore the force pushes until it works!)