diff --git a/index.d.ts b/index.d.ts index 843f8f9..f812a4c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -31,7 +31,7 @@ export function getProperty | unknown[] ? (Get extends unknown ? DefaultValue : Get) : undefined; +): ObjectType extends Record | unknown[] ? (unknown extends Get ? DefaultValue : Get) : undefined; /** Set the property at the given path to the given value. diff --git a/index.test-d.ts b/index.test-d.ts index d33e872..53d859c 100644 --- a/index.test-d.ts +++ b/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(getProperty({foo: {bar: 'unicorn'}}, 'foo.bar')); -expectType(getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep')); -expectAssignable( +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( +).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(); const object = {foo: {bar: 'a'}}; -expectType(setProperty(object, 'foo.bar', 'b')); +expectTypeOf(setProperty(object, 'foo.bar', 'b')).toEqualTypeOf(object); -expectType(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar')); +expectTypeOf(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toEqualTypeOf(); -expectType(deleteProperty({foo: {bar: 'a'}}, 'foo.bar')); +expectTypeOf(deleteProperty({foo: {bar: 'a'}}, 'foo.bar')).toEqualTypeOf(); diff --git a/package.json b/package.json index 697cc4b..bb3b97a 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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" } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0c9966a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "lib": [ + "es2020" + ], + "strict": true, + "noEmit": true + }, + "include": [ + "*.ts" + ] +}