Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to TypeScript and ensure strict type safety #101

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

XDGFX
Copy link

@XDGFX XDGFX commented Mar 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant