Skip to content

Commit

Permalink
Require TypeScript 4.1 and add strongly-typed TypeScript types (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Richienb and sindresorhus committed Mar 22, 2021
1 parent 886ba13 commit 09adad9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
26 changes: 12 additions & 14 deletions 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.
Expand All @@ -23,15 +25,11 @@ declare const dotProp: {
//=> 'unicorn'
```
*/
get<T>(
object: {[key: string]: any} | undefined,
path: string
): T | undefined;
get<T>(
object: {[key: string]: any} | undefined,
path: string,
defaultValue: T
): T;
get: <ObjectType, PathType extends string, DefaultValue = undefined>(
object: ObjectType,
path: PathType,
defaultValue?: DefaultValue
) => ObjectType extends Record<string, unknown> ? (Get<ObjectType, PathType> extends unknown ? DefaultValue : Get<ObjectType, PathType>) : undefined; // TODO: When adding array index support (https://github.com/sindresorhus/dot-prop/issues/71) add ` | unknown[]` after `Record<string, unknown>`

/**
Set the property at the given path to the given value.
Expand Down Expand Up @@ -59,11 +57,11 @@ declare const dotProp: {
//=> {foo: {bar: 'b', baz: 'x'}}
```
*/
set<T extends {[key: string]: any}>(
object: T,
set: <ObjectType extends {[key: string]: any}>(
object: ObjectType,
path: string,
value: unknown
): T;
) => ObjectType;

/**
Check whether the property at the given path exists.
Expand All @@ -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.
Expand All @@ -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;
7 changes: 3 additions & 4 deletions index.test-d.ts
@@ -1,13 +1,12 @@
import {expectType, expectAssignable} from 'tsd';
import dotProp = require('.');

expectType<unknown>(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar'));
expectType<string | undefined>(dotProp.get<string>({foo: {bar: 'unicorn'}}, 'foo.bar'));
expectType<unknown>(dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep'));
expectType<string>(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar'));
expectType<undefined>(dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep'));
expectAssignable<string>(
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value')
);
expectType<unknown>(
expectType<string>(
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot')
);

Expand Down
10 changes: 3 additions & 7 deletions package.json
Expand Up @@ -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"
}
}
}

0 comments on commit 09adad9

Please sign in to comment.