Skip to content

Commit

Permalink
Fix TypeScript types (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
mmkal and sindresorhus committed Feb 16, 2022
1 parent bf3c2a6 commit 2c1bbfb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -31,7 +31,7 @@ export function getProperty<ObjectType, PathType extends string, DefaultValue =
object: ObjectType,
path: PathType,
defaultValue?: DefaultValue
): ObjectType extends Record<string, unknown> | unknown[] ? (Get<ObjectType, PathType> extends unknown ? DefaultValue : Get<ObjectType, PathType>) : undefined;
): ObjectType extends Record<string, unknown> | unknown[] ? (unknown extends Get<ObjectType, PathType> ? DefaultValue : Get<ObjectType, PathType>) : undefined;

/**
Set the property at the given path to the given value.
Expand Down
21 changes: 11 additions & 10 deletions index.test-d.ts
@@ -1,18 +1,19 @@
import {expectType, expectAssignable} from 'tsd';
import {expectTypeOf} from 'expect-type';
import {getProperty, setProperty, hasProperty, deleteProperty} from './index.js';

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

const object = {foo: {bar: 'a'}};
expectType<typeof object>(setProperty(object, 'foo.bar', 'b'));
expectTypeOf(setProperty(object, 'foo.bar', 'b')).toEqualTypeOf(object);

expectType<boolean>(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar'));
expectTypeOf(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toEqualTypeOf<boolean>();

expectType<boolean>(deleteProperty({foo: {bar: 'a'}}, 'foo.bar'));
expectTypeOf(deleteProperty({foo: {bar: 'a'}}, 'foo.bar')).toEqualTypeOf<boolean>();
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -16,7 +16,7 @@
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd",
"test": "xo && ava && tsc",
"bench": "node benchmark.js"
},
"files": [
Expand All @@ -42,7 +42,8 @@
"devDependencies": {
"ava": "^4.0.1",
"benchmark": "^2.1.4",
"tsd": "^0.19.1",
"expect-type": "^0.13.0",
"typescript": "^4.5.5",
"xo": "^0.47.0"
}
}
12 changes: 12 additions & 0 deletions tsconfig.json
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": [
"es2020"
],
"strict": true,
"noEmit": true
},
"include": [
"*.ts"
]
}

0 comments on commit 2c1bbfb

Please sign in to comment.