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

Internalize zen-observable types and drop @types/zen-observable dependency #152

Merged
merged 4 commits into from
Aug 24, 2021

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Aug 24, 2021

This is another approach to solving apollographql/apollo-client#8688 entirely within the zen-observable-ts package, which does not involve first converting the codebase to TypeScript as #151 attempted (so far unsuccessfully) to do.

As I wrote in apollographql/apollo-client#8688 (comment), I believe this is a problem zen-observable-ts can and should solve within itself, without @apollo/client having to declare a dependency on @types/zen-observable in order to use types from zen-observable-ts.

Unfortunately, as long as zen-observable-ts uses types from @types/zen-observable, it appears some (yarn v2-using) consumers of zen-observable-ts will be forced to install @types/zen-observable as a direct dependency of their own. This includes the @apollo/client package itself, which depends directly on zen-observable-ts and uses/exposes the Observable type it exports in many places.

To solve this problem, this PR removes @types/zen-observable from the picture, and internalizes the .d.ts files that it provided in module.d.ts, without global declarations like the ZenObservable namespace or Symbol.observable.

Note that zen-observable itself is not a TypeScript project, so @types/zen-observable is merely a description of its existing JavaScript API, rather than a generated artifact of the zen-observable build process. In that sense, internalizing those types within zen-observable-ts seems safe and legitimate, since these types are just another description of what zen-observable provides.

Note that @brainkim and I previously internalized the implementation of zen-observable/esm.js in PR #105, so now the types are following suit.

This allows us to remove the `@types/zen-observable` package entirely,
which should help with apollographql/apollo-client#8688.
I don't think we need this, as long as TypeScript doesn't complain about
our use of `Symbol.observable` in the `.d.ts` files.

Apollo Client imports a `symbol-observable` polyfill here:
https://github.com/apollographql/apollo-client/blob/main/src/utilities/observables/Observable.ts
We implemented these methods explicitly in PR #1, but then PR #105 went
back to compiling `Observable` down to ES5 constructor functions.

In other words, the current implementations of `Observable` (both
CommonJS and ESM) automatically have these static methods, and they work
just like `Function.prototype.{call,apply}`, because `Observable` is
compiled in both cases from a `class` to a constructor function.
@brainkim
Copy link
Contributor

Makes sense LGTM.

@benjamn benjamn merged commit 0327ae6 into main Aug 24, 2021
@benjamn benjamn deleted the internalize-zen-observable-types branch August 24, 2021 17:20
benjamn added a commit that referenced this pull request Aug 24, 2021
Minor version bump due to potential refactoring risk from PR #152. This
version will be published as `next` until we're confident we can promote
it to `latest`.
benjamn added a commit to apollographql/apollo-client that referenced this pull request Aug 24, 2021
benjamn added a commit to apollographql/apollo-client that referenced this pull request Aug 24, 2021
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.

2 participants