Skip to content

Commit

Permalink
[TS] added wrapper for atomic types
Browse files Browse the repository at this point in the history
  • Loading branch information
kosich committed Oct 10, 2020
1 parent fde934f commit fea639a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
18 changes: 15 additions & 3 deletions src/proxify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,24 @@ export function proxify<O>(o: Observable<O>): Proxify<O> {
export type Proxify<O> =
// O is function ?
O extends (...args: any[]) => infer R
? Proxy <O> & ICallableProxiedObservable<O, R>
: Proxy <O> & IProxiedObservable<O>
? Proxy<O> & ICallableProxiedObservable<O, R>
: Proxy<O> & IProxiedObservable<O>
;

// Basic proxy with props as proxify
export type Proxy<O> = { [P in keyof O]: Proxify<O[P]> };
type Proxy<O> =
O extends boolean
? { [P in keyof Boolean]: Proxify<Boolean[P]> }
: O extends string
? { [P in keyof String]: Proxify<String[P]> }
: O extends number
? { [P in keyof Number]: Proxify<Number[P]> }
: O extends bigint
? { [P in keyof BigInt]: Proxify<BigInt[P]> }
: O extends symbol
? { [P in keyof Symbol]: Proxify<Symbol[P]> }
// Object
: { [P in keyof O]: Proxify<O[P]> };

// Callable Proxied Observable
interface ICallableProxiedObservable<O extends (...args: any[]) => R, R> extends IProxiedObservable<O> {
Expand Down
37 changes: 19 additions & 18 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ describe('Proxify', () => {
const p = proxify(o);
expect(p.a === p.a).toBe(true);
});
});

describe('Types', () => {
// TS:
// proxify(fn)() -- should be Proxify
test('Fn call result should be of type Proxify', () => {
Expand All @@ -153,23 +155,22 @@ describe('Proxify', () => {
});

// TYPES: proxify(of('a', 'b')).length -- should be Proxify
// test('elemental types should be Proxify', () => {
// const o = of('Hello', 'World');
// const p = proxify(o);
// p.length.subscribe(console.log);
// });

// This test fails on typecheck due to `a` being
// (x: any, y: any) => { b: any }
// any type on b seem to corrupt further typings
// TODO: improve typings
// it('should call proxify on result w/ any', () => {
// const a = (x: any, y: any) => ({ b: x + y });
// const o = of({ a }, { a }, { a });
// const p = proxify(o);
// p.a(1, 1).b.subscribe(observer);
// expect(observer.next.mock.calls).toEqual([[1], [2], [3]])
// expect(observer.complete.mock.calls.length).toBe(1);
// });
test('atomic props should be Proxify', () => {
const o = of('Hi', 'World');
const p = proxify(o);
// crazy subtype
p.length.toString()[0].subscribe(observer);
expect(observer.next).toHaveBeenCalledWith('2');
expect(observer.next).toHaveBeenCalledWith('5');
});

it('should call proxify on result w/ any', () => {
const a = (x: any, y: any) => ({ b: x + y });
const o = of({ a }, { a }, { a });
const p = proxify(o);
p.a(1, 1).b.subscribe(observer);
expect(observer.next.mock.calls).toEqual([[2], [2], [2]])
expect(observer.complete.mock.calls.length).toBe(1);
});
});
});

0 comments on commit fea639a

Please sign in to comment.