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..915ed02 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,13 +1,12 @@ 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') ); diff --git a/package.json b/package.json index e1b01f3..31a0222 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": "^1.0.0" }, "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" - } } }