From eb2b37f573f85b5c0c7073596a30d28ac3ec650a Mon Sep 17 00:00:00 2001 From: planttheidea Date: Tue, 9 Jul 2019 06:48:41 -0700 Subject: [PATCH] strict mode for types --- __tests__/index.ts | 38 +++++++++++++++++++------------------- __tests__/utils.ts | 18 +++++++++++------- package.json | 5 +++-- src/handlers.ts | 11 ++++++----- tsconfig.json | 1 + 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/__tests__/index.ts b/__tests__/index.ts index aa505e6..92338b4 100644 --- a/__tests__/index.ts +++ b/__tests__/index.ts @@ -90,7 +90,7 @@ describe('add', () => { }); it('should add to the array object directly if an empty path', () => { - const path: null = null; + const path: any = null; const value = 'value'; const object: any[] = []; @@ -101,7 +101,7 @@ describe('add', () => { }); it('should return the value directly if the object is not an array and path is empty', () => { - const path: null = null; + const path: any = null; const value = 'value'; const object = {}; @@ -193,7 +193,7 @@ describe('addWith', () => { it('should add to the array object directly if an empty path', () => { const fn = (value: any) => ({ value }); - const path: null = null; + const path: any = null; const object: any[] = []; const result = addWith(fn, path, object); @@ -208,7 +208,7 @@ describe('addWith', () => { it('should return the value directly if the object is not an array and path is empty', () => { const fn = (value: any) => ({ value }); - const path: null = null; + const path: any = null; const object = {}; const result = addWith(fn, path, object); @@ -667,7 +667,7 @@ describe('assignWith', () => { }); it('should return the original objects if the path is empty and the fn returns falsy', () => { - const fn = (value: any): object => null; + const fn = (value: any): any => null; const path: any[] = []; const object = { untouched: true }; @@ -677,7 +677,7 @@ describe('assignWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object = {}; @@ -716,7 +716,7 @@ describe('call', () => { const parameters: any[] = [123, null]; const context = { iam: 'context' }; - const fn = jest.fn().mockImplementation(function () { + const fn = jest.fn().mockImplementation(function (this: any) { expect(this).toBe(context); return 'called'; @@ -864,7 +864,7 @@ describe('callWith', () => { const parameters: any[] = [123, null]; const context: object = { iam: 'context' }; - const fn = jest.fn().mockImplementation(function () { + const fn = jest.fn().mockImplementation(function (this: any) { expect(this).toBe(context); return 'called'; @@ -1062,7 +1062,7 @@ describe('callWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path: any[] = []; const parameters: any[] = [123, null]; const object = jest.fn().mockReturnValue('called'); @@ -1366,7 +1366,7 @@ describe('getWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; @@ -1528,7 +1528,7 @@ describe('getWithOr', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; const fallbackValue: string = 'fallback'; @@ -1749,7 +1749,7 @@ describe('hasWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; @@ -2095,7 +2095,7 @@ describe('isWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; const value: any = 'bar'; @@ -2555,7 +2555,7 @@ describe('mergeWith', () => { }); it('should return the original objects if the path is empty and the fn returns falsy', () => { - const fn = (value: any): object => null; + const fn = (value: any): any => null; const path: any[] = []; const object: unchanged.Unchangeable = { untouched: true }; @@ -2565,7 +2565,7 @@ describe('mergeWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; @@ -2573,7 +2573,7 @@ describe('mergeWith', () => { }); it('should return the value returned by fn if the object is not cloneable', () => { - const fn = (value: any): object => null; + const fn = (value: any): any => null; const path: any[] = []; const value: any = { foo: 'bar' }; const object: any = 123; @@ -2922,7 +2922,7 @@ describe('notWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; const value: any = 'bar'; @@ -3186,7 +3186,7 @@ describe('removeWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; @@ -3411,7 +3411,7 @@ describe('setWith', () => { }); it('should throw if the function passed is not a function', () => { - const fn: null = null; + const fn: any = null; const path = ['foo']; const object: unchanged.Unchangeable = {}; diff --git a/__tests__/utils.ts b/__tests__/utils.ts index f468e9e..641d2fc 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -219,7 +219,7 @@ describe('getDeepClone', () => { const value = 'value'; const path = 'deeply[0].nested'; - const object: null = null; + const object: any = null; const callback = jest.fn().mockImplementation((ref: unchanged.Unchangeable, key: string) => { expect(ref).toEqual({}); expect(key).toEqual(path.split('.')[1]); @@ -278,10 +278,10 @@ describe('getFullPath', () => { expect(result).toEqual(path); }); - it('should return the added index if path is an empty string and value is an array', () => { + it('should return the added index if path is null and value is an array', () => { const path: any = null; const object: any[] = []; - const fn: void = undefined; + const fn: any = undefined; const result = getFullPath(path, object, fn); @@ -348,7 +348,9 @@ describe('getOwnProperties', () => { const result = getOwnProperties(object); - expect(result).toEqual([].concat(Object.keys(object), [symbol])); + const keys = Object.keys(object); + + expect(result).toEqual(keys.concat([(symbol as unknown) as string])); }); it('should get only keys if no symbols in the object passed exist', () => { @@ -376,7 +378,9 @@ describe('getOwnProperties', () => { const result = getOwnProperties(object); - expect(result).toEqual([].concat(Object.keys(object), [symbol])); + const keys = Object.keys(object); + + expect(result).toEqual(keys.concat([(symbol as unknown) as string])); }); }); @@ -646,7 +650,7 @@ describe('getValueAtPath', () => { it('should return undefined when the object does not exist', () => { const path = 'path'; - const object: null = null; + const object: any = null; const fallbackValue: undefined = undefined; const result: void = getValueAtPath(path, object, fallbackValue); @@ -656,7 +660,7 @@ describe('getValueAtPath', () => { it('should return the fallback when the object does not exist and a fallback is provided', () => { const path = 'path'; - const object: null = null; + const object: any = null; const fallbackValue = 'fallback'; const result: void = getValueAtPath(path, object, fallbackValue); diff --git a/package.json b/package.json index 622cb6e..32ce7d3 100644 --- a/package.json +++ b/package.json @@ -69,11 +69,12 @@ "lint": "NODE_ENV=test tslint 'src/*.ts'", "lint:fix": "npm run lint -- --fix", "prepublish": "if in-publish; then npm run prepublish:compile; fi", - "prepublish:compile": "npm run lint && npm run test:coverage && npm run dist", + "prepublish:compile": "npm run lint && npm run typecheck && npm run test:coverage && npm run dist", "start": "npm run dev", "test": "NODE_PATH=. jest", "test:coverage": "npm run test -- --coverage", - "test:watch": "npm run test -- --watch" + "test:watch": "npm run test -- --watch", + "typecheck": "tsc --noEmit" }, "sideEffects": false, "types": "dist/index.d.ts", diff --git a/src/handlers.ts b/src/handlers.ts index b3a9dd3..549dd03 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -288,7 +288,7 @@ export function createMerge( const result: unchanged.Unchangeable = getDeepClone( path, object, - (ref: unchanged.Unchangeable, key: string): void => { + (ref: unchanged.Unchangeable, key: unchanged.PathItem): void => { const objectToMerge: any = fn(ref[key], ...extraArgs); if (objectToMerge) { @@ -317,7 +317,7 @@ export function createMerge( : getDeepClone( path, object, - (ref: unchanged.Unchangeable, key: string): void => { + (ref: unchanged.Unchangeable, key: unchanged.PathItem): void => { ref[key] = getMergedObject(ref[key], objectToMerge, isDeep); }, ); @@ -341,7 +341,7 @@ export function createNot( ): unchanged.NotWith | unchanged.Not { const is: Function = createIs(isWithHandler); - return function not() { + return function not(this: any) { return !is.apply(this, arguments); }; } @@ -453,7 +453,7 @@ export function createSet( : getDeepClone( path, object, - (ref: unchanged.Unchangeable, key: string): void => { + (ref: unchanged.Unchangeable, key: unchanged.PathItem): void => { ref[key] = fn(ref[key], ...extraArgs); }, ); @@ -470,7 +470,7 @@ export function createSet( : getDeepClone( path, object, - (ref: unchanged.Unchangeable, key: string): void => { + (ref: unchanged.Unchangeable, key: unchanged.PathItem): void => { ref[key] = value; }, ); @@ -496,6 +496,7 @@ export function createAdd( if (isWithHandler) { return function addWith( + this: any, fn: unchanged.WithHandler, path: unchanged.Path, object: unchanged.Unchangeable, diff --git a/tsconfig.json b/tsconfig.json index 80fc666..f952fd6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,7 @@ "noImplicitAny": true, "outDir": "./dist", "sourceMap": true, + "strict": true, "target": "es5", "types": ["jest", "node", "react"] },