deep-object-diff
deep-object-diff copied to clipboard
Convert to TypeScript and ensure strict type safety
Overview
Inspired by PR #98 and #99, and with the desire to use Typia functions which means types must be properly defined, I have ported all the JS functions to fully typed TS.
The only function signature change is in src/utils.ts, where the hasOwnProperty method expects a PropertyKey parameter, so I updated the function to expect that type only.
No other function signatures are changed.
The benefit is that for all functions, TS users have a reliable type output based on the types supplied as inputs. No change to JS users.
Ensured that build, lint, test, and test:coverage all pass without errors. Replaced yarn commands with npm as yarn was not specified as a devDependency.
Types
The main types returned by the various diff functions are now defined as follows:
export type DiffDeletedType<T, U> =
| {
[P in Exclude<keyof ElementType<T>, keyof ElementType<U>>]: undefined;
}
| EmptyObject;
export type DiffAddedType<T, U> =
| {
[P in Exclude<
keyof ElementType<U>,
keyof ElementType<T>
>]: ElementType<U>[P];
}
| EmptyObject;
export type DiffUpdatedType<T, U> =
| {
[P in keyof ElementType<T> & keyof ElementType<U>]?: DiffUpdatedType<
ElementType<T>[P],
ElementType<U>[P]
>;
}
| U
| EmptyObject;
export type DiffType<T, U> =
| U
| (DiffAddedType<T, U> & DiffDeletedType<T, U> & DiffUpdatedType<T, U>);
The types were determined by reading the function code and ensuring the returned values match the specified type.