From 7563def4105dc20bd32cf524b5f91f55be3d622b Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Thu, 4 Mar 2021 11:50:00 +1300 Subject: [PATCH 1/4] Require TypeScript 4.1 and add strongly-typed TypeScript types Signed-off-by: Richie Bendall --- index.d.ts | 26 ++++++++++++-------------- index.test-d.ts | 8 ++++---- package.json | 12 ++++-------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2976791..7fbb14b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,5 @@ +import {Get} from 'type-fest'; + declare const dotProp: { /** Get the value of the property at the given path. @@ -23,15 +25,11 @@ declare const dotProp: { //=> 'unicorn' ``` */ - get( - object: {[key: string]: any} | undefined, - path: string - ): T | undefined; - get( - object: {[key: string]: any} | undefined, - path: string, - defaultValue: T - ): T; + get: ( + object: ObjectType, + path: PathType, + defaultValue?: DefaultValue + ) => ObjectType extends Record ? (Get extends unknown ? DefaultValue : Get) : undefined; // TODO: When adding array index support (https://github.com/sindresorhus/dot-prop/issues/71) add ` | unknown[]` after `Record` /** Set the property at the given path to the given value. @@ -59,11 +57,11 @@ declare const dotProp: { //=> {foo: {bar: 'b', baz: 'x'}} ``` */ - set( - object: T, + set: ( + object: ObjectType, path: string, value: unknown - ): T; + ) => ObjectType; /** Check whether the property at the given path exists. @@ -79,7 +77,7 @@ declare const dotProp: { //=> true ``` */ - has(object: {[key: string]: any} | undefined, path: string): boolean; + has: (object: {[key: string]: any} | undefined, path: string) => boolean; /** Delete the property at the given path. @@ -103,7 +101,7 @@ declare const dotProp: { //=> {foo: {bar: {y: 'x'}}} ``` */ - delete(object: {[key: string]: any}, path: string): boolean; + delete: (object: {[key: string]: any}, path: string) => boolean; }; export = dotProp; diff --git a/index.test-d.ts b/index.test-d.ts index 996f20c..2d4891b 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,15 +1,15 @@ import {expectType, expectAssignable} from 'tsd'; import dotProp = require('.'); -expectType(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar')); -expectType(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar')); -expectType(dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep')); +expectType(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar')); +expectType(dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep')); expectAssignable( dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value') ); -expectType( +expectType( dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot') ); +expectType(dotProp.get(['a', 'b', 'c'], '0')); const object = {foo: {bar: 'a'}}; expectType(dotProp.set(object, 'foo.bar', 'b')); diff --git a/package.json b/package.json index e1b01f3..4998c02 100644 --- a/package.json +++ b/package.json @@ -35,17 +35,13 @@ "dotty" ], "dependencies": { - "is-obj": "^2.0.0" + "is-obj": "^2.0.0", + "type-fest": "^0.21.2" }, "devDependencies": { "ava": "^2.1.0", "benchmark": "^2.1.4", - "tsd": "^0.13.1", + "tsd": "^0.14.0", "xo": "^0.33.1" - }, - "xo": { - "rules": { - "@typescript-eslint/method-signature-style": "off" - } } -} +} \ No newline at end of file From 9117880ead6bd424c3b0c22c07fa5888e03719e6 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Thu, 4 Mar 2021 11:52:26 +1300 Subject: [PATCH 2/4] Remove array test Signed-off-by: Richie Bendall --- index.test-d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/index.test-d.ts b/index.test-d.ts index 2d4891b..915ed02 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -9,7 +9,6 @@ expectAssignable( expectType( dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot') ); -expectType(dotProp.get(['a', 'b', 'c'], '0')); const object = {foo: {bar: 'a'}}; expectType(dotProp.set(object, 'foo.bar', 'b')); From 5721dfa0fc8e2d7dfd0f0d4d7f04e92aec83a347 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 22 Mar 2021 17:10:52 +0700 Subject: [PATCH 3/4] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4998c02..6438a63 100644 --- a/package.json +++ b/package.json @@ -44,4 +44,4 @@ "tsd": "^0.14.0", "xo": "^0.33.1" } -} \ No newline at end of file +} From a91664088d4c2b058991202b8ba93ac080fab252 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 22 Mar 2021 17:12:33 +0700 Subject: [PATCH 4/4] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6438a63..31a0222 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ ], "dependencies": { "is-obj": "^2.0.0", - "type-fest": "^0.21.2" + "type-fest": "^1.0.0" }, "devDependencies": { "ava": "^2.1.0",