Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require TypeScript 4.1 and add strongly-typed TypeScript types #80

Merged
merged 4 commits into from Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
}
}