Skip to content

Commit eb1d596

Browse files
committed
fix(types): support passing union types to from
1 parent 5aea50e commit eb1d596

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

spec-dtslint/observables/from-spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { from, of, asyncScheduler } from 'rxjs';
2+
3+
it('should accept an array', () => {
4+
const o = from([1, 2, 3, 4]); // $ExpectType Observable<number>
5+
});
6+
7+
it('should accept a Promise', () => {
8+
const o = from(Promise.resolve('test')); // $ExpectType Observable<string>
9+
});
10+
11+
it('should accept an Iterable', () => {
12+
const iterable = (function*() {
13+
yield 42;
14+
}());
15+
16+
const o = from(iterable); // $ExpectType Observable<number>
17+
});
18+
19+
it('should accept an Observable', () => {
20+
const o = from(of('test')); // $ExpectType Observable<string>
21+
});
22+
23+
it('should accept union types', () => {
24+
const o = from(Math.random() > 0.5 ? of(123) : of('test')); // $ExpectType Observable<string | number>
25+
});
26+
27+
it('should accept Observable<Observable<number>>', () => {
28+
const o = from(of(of(123))); // $ExpectType Observable<Observable<number>>
29+
});
30+
31+
it('should accept Observable<number[]>', () => {
32+
const o = from(of([1, 2, 3])); // $ExpectType Observable<number[]>
33+
});
34+
35+
it('should accept an array of Observables', () => {
36+
const o = from([of(1), of(2), of(3)]); // $ExpectType Observable<Observable<number>>
37+
});
38+
39+
it('should accept an array of Inputs', () => {
40+
const iterable = (function*() {
41+
yield 42;
42+
}());
43+
44+
const o = from([of(1), ['test'], iterable]); // $ExpectType Observable<Observable<number> | IterableIterator<number> | string[]>
45+
});

src/internal/observable/from.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import { fromPromise } from './fromPromise';
88
import { fromIterable } from './fromIterable';
99
import { fromObservable } from './fromObservable';
1010
import { subscribeTo } from '../util/subscribeTo';
11-
import { ObservableInput, SchedulerLike } from '../types';
11+
import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
1212

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

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

0 commit comments

Comments
 (0)