Skip to content

Commit 2c1bbfb

Browse files
mmkalsindresorhus
andauthoredFeb 16, 2022
Fix TypeScript types (#92)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent bf3c2a6 commit 2c1bbfb

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function getProperty<ObjectType, PathType extends string, DefaultValue =
3131
object: ObjectType,
3232
path: PathType,
3333
defaultValue?: DefaultValue
34-
): ObjectType extends Record<string, unknown> | unknown[] ? (Get<ObjectType, PathType> extends unknown ? DefaultValue : Get<ObjectType, PathType>) : undefined;
34+
): ObjectType extends Record<string, unknown> | unknown[] ? (unknown extends Get<ObjectType, PathType> ? DefaultValue : Get<ObjectType, PathType>) : undefined;
3535

3636
/**
3737
Set the property at the given path to the given value.

‎index.test-d.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import {expectType, expectAssignable} from 'tsd';
1+
import {expectTypeOf} from 'expect-type';
22
import {getProperty, setProperty, hasProperty, deleteProperty} from './index.js';
33

4-
expectType<string>(getProperty({foo: {bar: 'unicorn'}}, 'foo.bar'));
5-
expectType<undefined>(getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep'));
6-
expectAssignable<string>(
4+
expectTypeOf(getProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toBeString();
5+
expectTypeOf(getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep')).toBeUndefined();
6+
expectTypeOf(
77
getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value'),
8-
);
9-
expectType<string>(
8+
).toBeString();
9+
expectTypeOf(
1010
getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot'),
11-
);
11+
// @ts-expect-error type-fest's `Get` not smart enough to deal with escaped dots
12+
).toEqualTypeOf<string>();
1213

1314
const object = {foo: {bar: 'a'}};
14-
expectType<typeof object>(setProperty(object, 'foo.bar', 'b'));
15+
expectTypeOf(setProperty(object, 'foo.bar', 'b')).toEqualTypeOf(object);
1516

16-
expectType<boolean>(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar'));
17+
expectTypeOf(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toEqualTypeOf<boolean>();
1718

18-
expectType<boolean>(deleteProperty({foo: {bar: 'a'}}, 'foo.bar'));
19+
expectTypeOf(deleteProperty({foo: {bar: 'a'}}, 'foo.bar')).toEqualTypeOf<boolean>();

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1717
},
1818
"scripts": {
19-
"test": "xo && ava && tsd",
19+
"test": "xo && ava && tsc",
2020
"bench": "node benchmark.js"
2121
},
2222
"files": [
@@ -42,7 +42,8 @@
4242
"devDependencies": {
4343
"ava": "^4.0.1",
4444
"benchmark": "^2.1.4",
45-
"tsd": "^0.19.1",
45+
"expect-type": "^0.13.0",
46+
"typescript": "^4.5.5",
4647
"xo": "^0.47.0"
4748
}
4849
}

‎tsconfig.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"es2020"
5+
],
6+
"strict": true,
7+
"noEmit": true
8+
},
9+
"include": [
10+
"*.ts"
11+
]
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.