Skip to content

Commit

Permalink
TypeScript: Add overload when ignoreSymbols and pathAsArray are t…
Browse files Browse the repository at this point in the history
…rue (#100)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Seldszar and sindresorhus committed Nov 20, 2022
1 parent a40d3b9 commit 6b23d3a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,20 @@ declare const onChange: {
options?: Options & {pathAsArray?: false}
): ObjectType;

// Overload that returns an Array as path when `pathAsArray` option is true.
// Overload that returns a string array as path when `ignoreSymbols` and `pathAsArray` options are true.
<ObjectType extends Record<string, any>>(
object: ObjectType,
onChange: (
this: ObjectType,
path: string[],
value: unknown,
previousValue: unknown,
applyData: ApplyData
) => void,
options: Options & {ignoreSymbols: true; pathAsArray: true}
): ObjectType;

// Overload that returns an array as path when `pathAsArray` option is true.
<ObjectType extends Record<string, any>>(
object: ObjectType,
onChange: (
Expand Down
11 changes: 11 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ const watchedObjectPathAsString = onChange(object, function (path) {
expectType<typeof object>(watchedObjectPathAsString);

watchedObjectPathAsString.foo = true;

const watchedObjectPathAsStringArray = onChange(object, function (path) {
expectType<typeof object>(this);
expectType<string[]>(path);
}, {
ignoreSymbols: true,
pathAsArray: true,
});
expectType<typeof object>(watchedObjectPathAsStringArray);

watchedObjectPathAsStringArray.foo = true;

0 comments on commit 6b23d3a

Please sign in to comment.