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

Copied typings from DefinitelyTyped #1092

Closed
wants to merge 1 commit into from
Closed

Copied typings from DefinitelyTyped #1092

wants to merge 1 commit into from

Conversation

JoshuaKGoldberg
Copy link

@JoshuaKGoldberg JoshuaKGoldberg commented Nov 23, 2017

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.

(note: I don't have make locally, so I'm using AppVeyor/Travis as my build machine. Ignore the force pushes until it works!) AppVeyor failures look unrelated. Can this get merged anyway? 😄

@JoshuaKGoldberg JoshuaKGoldberg requested a review from a team as a code owner November 23, 2017 07:34
@codecov
Copy link

codecov bot commented Nov 23, 2017

Codecov Report

Merging #1092 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           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.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9c1a99f...24d0aea. Read the comment docs.

keithamus
keithamus previously approved these changes Nov 23, 2017
Copy link
Member

@keithamus keithamus left a 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.

🎉

@demurgos
Copy link

demurgos commented Nov 23, 2017

@keithamus
This looks like a good start effectively, but I am not sure if it is safe to merge yet due to the type reference to assertion-error.

Copy link

@demurgos demurgos left a 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"/>

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.

Copy link
Member

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)

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 to package.json (quicker)

Copy link
Member

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.

Copy link
Author

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...

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/assertion-error/index.d.ts

+1 to adding type definitions to assertion-error and -1 to adding any extra dependencies to package.json. I'll tackle that now.

Copy link
Author

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.
@JoshuaKGoldberg
Copy link
Author

JoshuaKGoldberg commented Dec 18, 2017

@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 :(

@demurgos
Copy link

demurgos commented Dec 18, 2017

Thanks for update. I checked the assertion-error typings and they seem good to merge. I'll take a look at this PR now.

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?)

@JoshuaKGoldberg
Copy link
Author

Huh, I was seeing a coveralls integration saying it was reduced. Never mind!

@demurgos
Copy link

demurgos commented Dec 18, 2017

I am having trouble to run the tests in your branch. Do you just use npm install && test-typescript? (module chai not found)

Edit: Solved, but I don't have much time right now. I'll submit a review later.

@demurgos
Copy link

demurgos commented Dec 19, 2017

Hi,
I added some commits on top of your branch: https://github.com/demurgos/chai/commits/types.

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 export = pattern (it's just a hack, with some drawbacks related to interop) but this is a lower priority issue that could be fixed in a subsequent PR. The exports should simply be defined as ES exports: (export const assert: AssertStatic)

@demurgos
Copy link

demurgos commented Jan 3, 2018

The types for assertion-error were merged and the version was bumped. We're waiting for the new version to be published to npm but it should happen soon.

I think I'll update my branch and send a new PR superseding this one once the types are on npm. (See previous message)

@meeber
Copy link
Contributor

meeber commented Jan 3, 2018

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?

@demurgos
Copy link

demurgos commented Jan 3, 2018

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:

  • Require type definitions to always document the current JS code (enforce synchronization)
  • Wait for Typescript users to send PRs to update the types when they need a feature.

You can also have a middle-ground were you declare methods and functions with the any type and let PRs refine the types.

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 plugin-error issue.

@meeber
Copy link
Contributor

meeber commented Jan 3, 2018

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?

@demurgos
Copy link

demurgos commented Jan 3, 2018

Right, I mainly addressed feature additions but not breaking changes.
What I am describing below are trade-offs that violate the purpose of types or semver in favor of easier maintenance. I don't think desynced types should be the norm but I will try to provide you the best description:

  • Incomplete types are the less harmful: if a function is not defined, TS users cannot use it but they'll just send a PR, they can bypass type-checking in the meantime. This happens when a feature is added and the types are not updated.
  • Types with extra symbols because of removal in the code without updating the types are more misleading. Still, they are usually found rather quickly and consumers can simply avoid to use them until the types are updated. This may be tolerated.
  • Invalid types due to changes in the function signatures or types of exported symbols are the worst and should be avoided. If a function requires a string argument but is changed to accept a number argument then TS users will have to manually bypass typechecking around this function to use it. This is usually not tolerated.

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).

demurgos pushed a commit to demurgos/chai that referenced this pull request Jan 7, 2018
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
@demurgos
Copy link

demurgos commented Jan 7, 2018

I updated my branch to use the new assertion-error version. I submitted a new PR where we can continue the discussion (based on this PR, with fixes and changes discussed above).

keithamus pushed a commit to demurgos/chai that referenced this pull request Mar 22, 2018
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
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.

5 participants