From fea639a93eab6e9f86be255f3e8421b8976aa08e Mon Sep 17 00:00:00 2001 From: Kostiantyn Palchyk Date: Sun, 11 Oct 2020 02:22:48 +0300 Subject: [PATCH] [TS] added wrapper for atomic types --- src/proxify.ts | 18 +++++++++++++++--- tests/index.test.ts | 37 +++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/proxify.ts b/src/proxify.ts index 0501d66..60d9701 100644 --- a/src/proxify.ts +++ b/src/proxify.ts @@ -102,12 +102,24 @@ export function proxify(o: Observable): Proxify { export type Proxify = // O is function ? O extends (...args: any[]) => infer R - ? Proxy & ICallableProxiedObservable - : Proxy & IProxiedObservable + ? Proxy & ICallableProxiedObservable + : Proxy & IProxiedObservable ; // Basic proxy with props as proxify -export type Proxy = { [P in keyof O]: Proxify }; +type Proxy = + O extends boolean + ? { [P in keyof Boolean]: Proxify } + : O extends string + ? { [P in keyof String]: Proxify } + : O extends number + ? { [P in keyof Number]: Proxify } + : O extends bigint + ? { [P in keyof BigInt]: Proxify } + : O extends symbol + ? { [P in keyof Symbol]: Proxify } + // Object + : { [P in keyof O]: Proxify }; // Callable Proxied Observable interface ICallableProxiedObservable R, R> extends IProxiedObservable { diff --git a/tests/index.test.ts b/tests/index.test.ts index 280b23f..c6326c2 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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', () => { @@ -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); + }); }); });