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
2 changes: 1 addition & 1 deletion src/observableTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ type ObservableFunctionChildren<T> = {
? T[K]
: t extends Observable
? t
: Observable<t> & (() => t)
: Observable<t> & T[K]
: T[K] & Observable<T[K]>;
};

Expand Down
40 changes: 40 additions & 0 deletions tests/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,46 @@ describe('observable', () => {
type ObservableFn2 = ReturnType<typeof withTypedComputed2>;
expectTypeOf<ObservableFn2['bool']['get']>().returns.toEqualTypeOf<number>();
});

it('issue #631: function child preserves argument signature', () => {
type AppState = {
// A function returning a non-void value previously had its argument signature
// stripped down to `() => boolean`, so calling it errored with
// `Expected 0 arguments, but got 1`.
centerMap: (coords: unknown) => boolean;
};

function withFunctionChild() {
return observable<AppState>({
centerMap: (coords) => !!coords,
});
}

type ObservableFn = ReturnType<typeof withFunctionChild>;

// The function's argument signature must be preserved.
expectTypeOf<ObservableFn['centerMap']>().toBeCallableWith('some-coords');
expectTypeOf<ObservableFn['centerMap']>().parameters.toEqualTypeOf<[coords: unknown]>();
});

it('issue #521: async function child without args keeps its Promise return type', () => {
type Example = {
// Without arguments the Promise return was wrongly unwrapped to `number`.
asyncNoArgs(): Promise<number>;
};

function withAsyncChildren() {
return observable<Example>({
async asyncNoArgs() {
return 1;
},
});
}

type ObservableFn = ReturnType<typeof withAsyncChildren>;

expectTypeOf<ObservableFn['asyncNoArgs']>().returns.toEqualTypeOf<Promise<number>>();
});
});

describe('Observable', () => {
Expand Down