Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/observable/src/observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ describe('Observable', () => {
for await (const value of source) {
results.push(value);
}
} catch (err: any) {
} catch (err: unknown) {
thrownError = err;
}

expect(thrownError instanceof Error).to.be.true;
expect(thrownError?.message).to.equal('wee');
expect(results).to.deep.equal([1, 2]);
});
Expand Down
119 changes: 62 additions & 57 deletions packages/observable/src/observable.ts

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions packages/observable/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export interface NextNotification<T> {
export interface ErrorNotification {
/** The kind of notification. Always "E" */
kind: 'E';
error: any;
error: unknown;
}

/**
Expand All @@ -151,21 +151,21 @@ export type ObservableNotification<T> = NextNotification<T> | ErrorNotification
export interface NextObserver<T> {
closed?: boolean;
next: (value: T) => void;
error?: (err: any) => void;
error?: (err: unknown) => void;
complete?: () => void;
}

export interface ErrorObserver<T> {
closed?: boolean;
next?: (value: T) => void;
error: (err: any) => void;
error: (err: unknown) => void;
complete?: () => void;
}

export interface CompletionObserver<T> {
closed?: boolean;
next?: (value: T) => void;
error?: (err: any) => void;
error?: (err: unknown) => void;
complete: () => void;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ export interface Observer<T> {
*
* For more info, please refer to {@link guide/glossary-and-semantics#error this guide}.
*/
error: (err: any) => void;
error: (err: unknown) => void;
/**
* A callback function that gets called by the producer if and when it has no more
* values to provide (by calling `next` callback function). This means that no error
Expand Down Expand Up @@ -237,15 +237,15 @@ export interface TimestampProvider {
}

/**
* Extracts the type from an `ObservableInput<any>`. If you have
* `O extends ObservableInput<any>` and you pass in `Observable<number>`, or
* Extracts the type from an `ObservableInput<unknown>`. If you have
* `O extends ObservableInput<unknown>` and you pass in `Observable<number>`, or
* `Promise<number>`, etc, it will type as `number`.
*/
export type ObservedValueOf<O> = O extends ObservableInput<infer T> ? T : never;

/**
* Extracts a union of element types from an `ObservableInput<any>[]`.
* If you have `O extends ObservableInput<any>[]` and you pass in
* Extracts a union of element types from an `ObservableInput<unknown>[]`.
* If you have `O extends ObservableInput<unknown>[]` and you pass in
* `Observable<string>[]` or `Promise<string>[]` you would get
* back a type of `string`.
* If you pass in `[Observable<string>, Observable<number>]` you would
Expand All @@ -254,8 +254,8 @@ export type ObservedValueOf<O> = O extends ObservableInput<infer T> ? T : never;
export type ObservedValueUnionFromArray<X> = X extends Array<ObservableInput<infer T>> ? T : never;

/**
* Extracts a tuple of element types from an `ObservableInput<any>[]`.
* If you have `O extends ObservableInput<any>[]` and you pass in
* Extracts a tuple of element types from an `ObservableInput<unknown>[]`.
* If you have `O extends ObservableInput<unknown>[]` and you pass in
* `[Observable<string>, Observable<number>]` you would get back a type
* of `[string, number]`.
*/
Expand All @@ -274,23 +274,23 @@ export type ObservableInputTuple<T> = {
* Constructs a new tuple with the specified type at the head.
* If you declare `Cons<A, [B, C]>` you will get back `[A, B, C]`.
*/
export type Cons<X, Y extends readonly any[]> = ((arg: X, ...rest: Y) => any) extends (...args: infer U) => any ? U : never;
export type Cons<X, Y extends readonly unknown[]> = ((arg: X, ...rest: Y) => unknown) extends (...args: infer U) => unknown ? U : never;

/**
* Extracts the head of a tuple.
* If you declare `Head<[A, B, C]>` you will get back `A`.
*/
export type Head<X extends readonly any[]> = ((...args: X) => any) extends (arg: infer U, ...rest: any[]) => any ? U : never;
export type Head<X extends readonly unknown[]> = ((...args: X) => unknown) extends (arg: infer U, ...rest: unknown[]) => unknown ? U : never;

/**
* Extracts the tail of a tuple.
* If you declare `Tail<[A, B, C]>` you will get back `[B, C]`.
*/
export type Tail<X extends readonly any[]> = ((...args: X) => any) extends (arg: any, ...rest: infer U) => any ? U : never;
export type Tail<X extends readonly unknown[]> = ((...args: X) => unknown) extends (arg: unknown, ...rest: infer U) => unknown ? U : never;

/**
* Extracts the generic value from an Array type.
* If you have `T extends Array<any>`, and pass a `string[]` to it,
* If you have `T extends Array<unknown>`, and pass a `string[]` to it,
* `ValueFromArray<T>` will return the actual type of `string`.
*/
export type ValueFromArray<A extends readonly unknown[]> = A extends Array<infer T> ? T : never;
Expand All @@ -299,7 +299,7 @@ export type ValueFromArray<A extends readonly unknown[]> = A extends Array<infer
* Gets the value type from an {@link ObservableNotification}, if possible.
*/
export type ValueFromNotification<T> = T extends { kind: 'N' | 'E' | 'C' }
? T extends NextNotification<any>
? T extends NextNotification<unknown>
? T extends { value: infer V }
? V
: undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/rxjs/spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ describe('Observable', () => {

it('should work with handlers with hacked bind methods, in the error case', () => {
const source = throwError(() => 'an error');
const results: any[] = [];
const error = function (value: string) {
const results: unknown[] = [];
const error = function (value: unknown) {
results.push(value);
};

Expand Down
6 changes: 3 additions & 3 deletions packages/rxjs/spec/Subject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ describe('Subject', () => {
results3.push(x);
},
error: function (err) {
expect(false).to.equal('should not throw error: ' + err.toString());
expect(false).to.equal(`should not throw error: ${err}`);
},
});

Expand Down Expand Up @@ -520,7 +520,7 @@ describe('Subject', () => {
it('should not next after error', () => {
const error = new Error('wut?');
const subject = new Subject<string>();
const results: string[] = [];
const results: unknown[] = [];
subject.subscribe({ next: (x) => results.push(x), error: (err) => results.push(err) });
subject.next('a');
subject.error(error);
Expand Down Expand Up @@ -596,7 +596,7 @@ describe('Subject', () => {

it('should not synchronously error when nexted into', (done) => {
config.onUnhandledError = (err) => {
expect(err.message).to.equal('Boom!');
expect(err instanceof Error && err.message).to.equal('Boom!');
done();
};

Expand Down
10 changes: 5 additions & 5 deletions packages/rxjs/spec/Subscriber-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('Subscriber', () => {
it('should report errors thrown from next', (done) => {
config.onUnhandledError = (err) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('test');
expect(err instanceof Error && err.message).to.equal('test');
done();
};

Expand All @@ -217,7 +217,7 @@ describe('Subscriber', () => {
it('should report errors thrown from complete', (done) => {
config.onUnhandledError = (err) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('test');
expect(err instanceof Error && err.message).to.equal('test');
done();
};

Expand All @@ -233,7 +233,7 @@ describe('Subscriber', () => {
it('should report errors thrown from error', (done) => {
config.onUnhandledError = (err) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('test');
expect(err instanceof Error && err.message).to.equal('test');
done();
};

Expand All @@ -249,7 +249,7 @@ describe('Subscriber', () => {
it('should report errors thrown from a full observer', (done) => {
config.onUnhandledError = (err) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('thrown from next');
expect(err instanceof Error && err.message).to.equal('thrown from next');
done();
};

Expand All @@ -271,7 +271,7 @@ describe('Subscriber', () => {
it('should report errors thrown from a full observer even if it is also shaped like a subscription', (done) => {
config.onUnhandledError = (err) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('thrown from next');
expect(err instanceof Error && err.message).to.equal('thrown from next');
done();
};

Expand Down
28 changes: 19 additions & 9 deletions packages/rxjs/spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @prettier */
import { expect } from 'chai';
import * as sinon from 'sinon';
import type { AjaxConfig, AjaxResponse} from 'rxjs/ajax';
import { ajax, AjaxError, AjaxTimeoutError } from 'rxjs/ajax';
import type { AjaxConfig } from 'rxjs/ajax';
import { ajax, AjaxError, AjaxResponse, AjaxTimeoutError } from 'rxjs/ajax';
import { TestScheduler } from 'rxjs/testing';
import { noop } from 'rxjs';
import * as nodeFormData from 'form-data';
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('ajax', () => {
next: () => {
done(new Error('should not be called'));
},
error: (e: Error) => {
error: (e) => {
expect(e).to.be.equal(expected);
done();
},
Expand Down Expand Up @@ -479,10 +479,15 @@ describe('ajax', () => {
};

ajax(obj).subscribe({
next: (x: any) => {
next: (x) => {
expect(x instanceof AjaxResponse).to.be.true;
if (!(x instanceof AjaxResponse))
return; // type-narrowing that @types/chai can't do

expect(x.status).to.equal(200);
expect(x.xhr.method).to.equal('GET');
expect(x.xhr.async).to.equal(true);
// fields only in MockXMLHttpRequest:
expect('method' in x.xhr && x.xhr.method).to.equal('GET');
expect('async' in x.xhr && x.xhr.async).to.equal(true);
expect(x.xhr.timeout).to.equal(10);
expect(x.xhr.responseType).to.equal('text');
},
Expand Down Expand Up @@ -515,9 +520,14 @@ describe('ajax', () => {
throw 'should not have been called';
},
error: (e) => {
expect(e instanceof AjaxTimeoutError).to.be.true;
if (!(e instanceof AjaxTimeoutError))
return; // type-narrowing that @types/chai can't do

expect(e.status).to.equal(0);
expect(e.xhr.method).to.equal('GET');
expect(e.xhr.async).to.equal(true);
// fields only in MockXMLHttpRequest:
expect('method' in e.xhr && e.xhr.method).to.equal('GET');
expect('async' in e.xhr && e.xhr.async).to.equal(true);
expect(e.xhr.timeout).to.equal(10);
expect(e.xhr.responseType).to.equal('text');
},
Expand Down Expand Up @@ -663,7 +673,7 @@ describe('ajax', () => {
next: () => {
done(new Error('should not be called'));
},
error: (e: Error) => {
error: (e) => {
expect(e).to.be.equal(expected);
done();
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rxjs/spec/observables/from-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ describe('from', () => {

from(erroringIterator).subscribe({
next: (x) => results.push(x),
error: (err) => results.push(err.message),
error: (err) => results.push(err instanceof Error && err.message),
});

expect(results).to.deep.equal([0, 1, 2, 'bad']);
Expand Down
2 changes: 1 addition & 1 deletion packages/rxjs/spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ describe('fromEvent', () => {
source.subscribe({
error: (err) => {
expect(err).to.be.an.instanceOf(TypeError);
expect(err.message).to.equal('Invalid event target');
expect(err instanceof TypeError && err.message).to.equal('Invalid event target');
done();
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/rxjs/spec/operators/tap-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ describe('tap', () => {
tap({
subscribe: () => results.push('subscribe'),
next: (value) => results.push(`next ${value}`),
error: (err) => results.push(`error: ${err.message}`),
error: (err) => results.push(`error: ${err}`),
complete: () => results.push('complete'),
unsubscribe: () => results.push('unsubscribe'),
finalize: () => results.push('finalize'),
Expand All @@ -351,7 +351,7 @@ describe('tap', () => {
tap({
subscribe: () => results.push('subscribe'),
next: (value) => results.push(`next ${value}`),
error: (err) => results.push(`error: ${err.message}`),
error: (err) => results.push(`error: ${err}`),
complete: () => results.push('complete'),
unsubscribe: () => results.push('unsubscribe'),
finalize: () => results.push('finalize'),
Expand All @@ -378,7 +378,7 @@ describe('tap', () => {
tap({
subscribe: () => results.push('subscribe'),
next: (value) => results.push(`next ${value}`),
error: (err) => results.push(`error: ${err.message}`),
error: (err) => results.push(`error: ${err}`),
complete: () => results.push('complete'),
unsubscribe: () => results.push('unsubscribe'),
finalize: () => results.push('finalize'),
Expand Down
4 changes: 2 additions & 2 deletions packages/rxjs/spec/subjects/ReplaySubject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('ReplaySubject', () => {
function feedNextIntoSubject(x: string) {
replaySubject.next(x);
}
function feedErrorIntoSubject(err: string) {
function feedErrorIntoSubject(err: unknown) {
replaySubject.error(err);
}
function feedCompleteIntoSubject() {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('ReplaySubject', () => {
function feedNextIntoSubject(x: string) {
replaySubject.next(x);
}
function feedErrorIntoSubject(err: string) {
function feedErrorIntoSubject(err: unknown) {
replaySubject.error(err);
}
function feedCompleteIntoSubject() {
Expand Down
4 changes: 2 additions & 2 deletions packages/rxjs/src/internal/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
hasError = false;

/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
thrownError: any = null;
thrownError: unknown = null;

constructor() {
// NOTE: This must be here to obscure Observable's constructor.
Expand All @@ -61,7 +61,7 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
}
}

error(err: any) {
error(err: unknown) {
if (!this._closed) {
this.hasError = this._closed = true;
this.thrownError = err;
Expand Down
Loading