Skip to content

Improve from #1528

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

Closed
wants to merge 3 commits into from
Closed

Conversation

david-driscoll
Copy link
Member

Description:
I tried to keep the changes down to a minimum here, if we feel like they need to split out into several PR's I'm okay with that. This may add pain for consumers that don't use the es6.lib.d.ts declarations. There is work on the TypeScript to help with this pain, see microsoft/TypeScript#6990 and microsoft/TypeScript#6974. Today we could point them at spec/es5.d.ts as a small shim they can use.

  1. Replaces Iterator<T> with Iterable<T>.
  2. Introduces Observablesque<T> which uses the symbol.

NOTE: The reason these are grouped is both 1 and 2, are changes that break compilation. We have to shim in SymbolConstructor and Iterable<T>.

Details:
Basically what's going on...

Right now we use Iterator<T> as part of ObservableInput<T> but really the interface we're looking for (in code that is) is Iterable<T>. So this change fixes that.

interface Iterable<T> {
    [Symbol.iterator](): Iterator<T>;
}

Also we declare Symbol.observable but this isn't declared anywhere in the interfaces, so there isn't an easy way to add that to ObservableInput<T> without an interface.

interface Iterable<T> {
[Symbol.iterator](): Iterator<T>;
}
interface Iterator<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's difference this to what we're currently using in es6-shim type definitions? looks identical.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

es6-shim doesn't actually shim in Symbol, so the IteratorShim<T> doesn't have the appropriate [Symbol.iterator]() attached to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I meant was Iterator specific. Sorry for confusion.

@david-driscoll david-driscoll force-pushed the improve-from branch 2 times, most recently from df137b4 to f19d06a Compare March 25, 2016 18:08
@david-driscoll
Copy link
Member Author

Rebased.

export type ArrayOrIterator<T> = Iterator<T> | ArrayLike<T>;
export type ObservableInput<T> = SubscribableOrPromise<T> | ArrayOrIterator<T>;
export interface Observablesque<T> {
[Symbol.observable](): Subscribable<T>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing here. [Symbol.observable]() should return a type that is Subscribable<T> AND Observablesque<T> at a minimum. (Similar to how Iterators have a Symbol.iterator on them)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know what? Scratch that. Don't worry about it, for the purposes of the internals of our library where this is being used, we don't care.

@benlesh
Copy link
Member

benlesh commented Mar 25, 2016

LGTM

@david-driscoll
Copy link
Member Author

The last thing I thought about, from a release perspective. Should we copy that es5.d.ts file into the dist outputs? That way if a user comes along with a project that breaks because they don't have Symbol, they can add /// <reference path="./node_modules/rxjs/es5.d.ts" /> to easily fix the problem.

I think at the time being this mainly applies to commonjs builds only I think.

@kwonoj
Copy link
Member

kwonoj commented Mar 25, 2016

@david-driscoll what about publish it into typings and let it be installed as same as other type shims?

@david-driscoll
Copy link
Member Author

I like the idea. Proposed name es6-rxjs-shim as it's specific to what rxjs needs.

I'll also add some notes to https://github.com/ReactiveX/rxjs/blob/master/doc/installation.md to direct users to install the typing if they are compiling their TypeScript project with es5 instead of es6.

@kwonoj
Copy link
Member

kwonoj commented Mar 25, 2016

I'll go through changes once again and check this in later today to tomorrow. type definition shim can be dealt with separate PR, I assume.

@david-driscoll
Copy link
Member Author

Sure thing, as long as the name works I'll start the work getting it into the typings registry in parallel.

@robwormald
Copy link
Contributor

Can we test this against core-js and the like? Anything that introduces types like Symbol to the global namespace is almost guaranteed to cause a huge amount of pain downstream...

@kwonoj
Copy link
Member

kwonoj commented Mar 26, 2016

@david-driscoll , let me hold off check in bit - would you able to clarify @robwormald 's request?

@david-driscoll
Copy link
Member Author

@robwormald does core-js have a like typescript typings? This change doesn't actually shim Symbol anywhere, instead it just allows us to use Symbol in es5 like code, so that our interfaces can accept a spec based observable.

The following just won't work, unless the TypeScript code has Symbol.

export interface Observablesque<T> {
  [Symbol.observable](): Subscribable<T>;
}

The problem is lib.d.ts does not have Symbol defined and lib.es6.d.ts does. Anyone target es6 from TypeScript will be able to consume this interface. The es5.d.ts (name can be changed) is so that someone targeting es5 from TypeScript will be able to still use the library. It is intentionally very slim and only introduces the types that rxjs needs to be able to consume Iterable<T> and Observablesque<T>.

interface Symbol { }
interface SymbolConstructor {
  iterator: symbol;
}
declare var Symbol: SymbolConstructor;
interface Iterable<T> {
    [Symbol.iterator](): Iterator<T>;
}
interface Iterator<T> {
    next(value?: any): IteratorResult<T>;
    return?(value?: any): IteratorResult<T>;
    throw?(e?: any): IteratorResult<T>;
}

@benlesh
Copy link
Member

benlesh commented Mar 28, 2016

Can we test this against core-js and the like?

Give me all of your PRs, @robwormald

@benlesh
Copy link
Member

benlesh commented Mar 28, 2016

@david-driscoll @kwonoj I labelled this "LGTM" a while ago... is there still work being done here?

@kwonoj
Copy link
Member

kwonoj commented Mar 29, 2016

Change itself is ready, wanted to ensure and clarify @robwormald 's question. I'm planning to check this in around tomorrow.

@david-driscoll
Copy link
Member Author

Along the lines of the discussion with @kwonoj and @robwormald I've created a typings repo, for es5.d.ts. I've added some content to the readme to explain the motivation behind it.

https://github.com/david-driscoll/rxjs-symbol-typings

If someone wants to move this to ReactiveX org, I'm happy to facilitate the move. Or just make a new repo, clone and push this content up, that's cool too.

Once this gets merged into the typings registry, I can create a follow up PR that removes es5.d.ts and uses the new typings. In addition I'll add a few notes to the getting started docs so that users targeting es5 will know that they need to have this additional typing installed to compile correctly.

@david-driscoll
Copy link
Member Author

rebased

@benlesh
Copy link
Member

benlesh commented Mar 29, 2016

@david-driscoll looks like there's still a build failure. Might want to dig into that.

@benlesh
Copy link
Member

benlesh commented Mar 29, 2016

Actually it looks like it has something to do with the merged mocha changes. cc/ @kwonoj

@david-driscoll
Copy link
Member Author

@Blesh Nah, it was my bad, I missed something on rebase... third time is the charm I think

@kwonoj
Copy link
Member

kwonoj commented Mar 29, 2016

Merged with c6ef2e8, thanks @david-driscoll

@kwonoj kwonoj closed this Mar 29, 2016
@lock
Copy link

lock bot commented Jun 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants