Skip to content

Commit

Permalink
fix(types): support passing union types to from
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jan 9, 2019
1 parent 5aea50e commit eb1d596
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
45 changes: 45 additions & 0 deletions spec-dtslint/observables/from-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { from, of, asyncScheduler } from 'rxjs';

it('should accept an array', () => {
const o = from([1, 2, 3, 4]); // $ExpectType Observable<number>
});

it('should accept a Promise', () => {
const o = from(Promise.resolve('test')); // $ExpectType Observable<string>
});

it('should accept an Iterable', () => {
const iterable = (function*() {
yield 42;
}());

const o = from(iterable); // $ExpectType Observable<number>
});

it('should accept an Observable', () => {
const o = from(of('test')); // $ExpectType Observable<string>
});

it('should accept union types', () => {
const o = from(Math.random() > 0.5 ? of(123) : of('test')); // $ExpectType Observable<string | number>
});

it('should accept Observable<Observable<number>>', () => {
const o = from(of(of(123))); // $ExpectType Observable<Observable<number>>
});

it('should accept Observable<number[]>', () => {
const o = from(of([1, 2, 3])); // $ExpectType Observable<number[]>
});

it('should accept an array of Observables', () => {
const o = from([of(1), of(2), of(3)]); // $ExpectType Observable<Observable<number>>
});

it('should accept an array of Inputs', () => {
const iterable = (function*() {
yield 42;
}());

const o = from([of(1), ['test'], iterable]); // $ExpectType Observable<Observable<number> | IterableIterator<number> | string[]>
});
5 changes: 2 additions & 3 deletions src/internal/observable/from.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { fromPromise } from './fromPromise';
import { fromIterable } from './fromIterable';
import { fromObservable } from './fromObservable';
import { subscribeTo } from '../util/subscribeTo';
import { ObservableInput, SchedulerLike } from '../types';
import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';

export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T>;
export function from<T>(input: ObservableInput<ObservableInput<T>>, scheduler?: SchedulerLike): Observable<Observable<T>>;
export function from<O extends ObservableInput<any>>(input: O, scheduler?: SchedulerLike): Observable<ObservedValueOf<O>>;

/**
* Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object.
Expand Down

0 comments on commit eb1d596

Please sign in to comment.