Skip to content

Commit 09adad9

Browse files
Richienbsindresorhus
andauthoredMar 22, 2021
Require TypeScript 4.1 and add strongly-typed TypeScript types (#80)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 886ba13 commit 09adad9

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed
 

‎index.d.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {Get} from 'type-fest';
2+
13
declare const dotProp: {
24
/**
35
Get the value of the property at the given path.
@@ -23,15 +25,11 @@ declare const dotProp: {
2325
//=> 'unicorn'
2426
```
2527
*/
26-
get<T>(
27-
object: {[key: string]: any} | undefined,
28-
path: string
29-
): T | undefined;
30-
get<T>(
31-
object: {[key: string]: any} | undefined,
32-
path: string,
33-
defaultValue: T
34-
): T;
28+
get: <ObjectType, PathType extends string, DefaultValue = undefined>(
29+
object: ObjectType,
30+
path: PathType,
31+
defaultValue?: DefaultValue
32+
) => 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>`
3533

3634
/**
3735
Set the property at the given path to the given value.
@@ -59,11 +57,11 @@ declare const dotProp: {
5957
//=> {foo: {bar: 'b', baz: 'x'}}
6058
```
6159
*/
62-
set<T extends {[key: string]: any}>(
63-
object: T,
60+
set: <ObjectType extends {[key: string]: any}>(
61+
object: ObjectType,
6462
path: string,
6563
value: unknown
66-
): T;
64+
) => ObjectType;
6765

6866
/**
6967
Check whether the property at the given path exists.
@@ -79,7 +77,7 @@ declare const dotProp: {
7977
//=> true
8078
```
8179
*/
82-
has(object: {[key: string]: any} | undefined, path: string): boolean;
80+
has: (object: {[key: string]: any} | undefined, path: string) => boolean;
8381

8482
/**
8583
Delete the property at the given path.
@@ -103,7 +101,7 @@ declare const dotProp: {
103101
//=> {foo: {bar: {y: 'x'}}}
104102
```
105103
*/
106-
delete(object: {[key: string]: any}, path: string): boolean;
104+
delete: (object: {[key: string]: any}, path: string) => boolean;
107105
};
108106

109107
export = dotProp;

‎index.test-d.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {expectType, expectAssignable} from 'tsd';
22
import dotProp = require('.');
33

4-
expectType<unknown>(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar'));
5-
expectType<string | undefined>(dotProp.get<string>({foo: {bar: 'unicorn'}}, 'foo.bar'));
6-
expectType<unknown>(dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep'));
4+
expectType<string>(dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar'));
5+
expectType<undefined>(dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep'));
76
expectAssignable<string>(
87
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value')
98
);
10-
expectType<unknown>(
9+
expectType<string>(
1110
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot')
1211
);
1312

‎package.json

+3-7
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@
3535
"dotty"
3636
],
3737
"dependencies": {
38-
"is-obj": "^2.0.0"
38+
"is-obj": "^2.0.0",
39+
"type-fest": "^1.0.0"
3940
},
4041
"devDependencies": {
4142
"ava": "^2.1.0",
4243
"benchmark": "^2.1.4",
43-
"tsd": "^0.13.1",
44+
"tsd": "^0.14.0",
4445
"xo": "^0.33.1"
45-
},
46-
"xo": {
47-
"rules": {
48-
"@typescript-eslint/method-signature-style": "off"
49-
}
5046
}
5147
}

0 commit comments

Comments
 (0)
Please sign in to comment.