Skip to content

Fix types for diffJson #610

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

Merged
merged 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 8.0.1

- [#610](https://github.com/kpdecker/jsdiff/pull/610) **Fixes types for `diffJson` which were broken by 8.0.0**. The new bundled types in 8.0.0 only allowed `diffJson` to be passed string arguments, but it should've been possible to pass either strings or objects (and now is). Thanks to Josh Kelley for the fix.

## 8.0.0

- [#580](https://github.com/kpdecker/jsdiff/pull/580) **Multiple tweaks to `diffSentences`**:
Expand Down
35 changes: 18 additions & 17 deletions src/diff/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,37 @@ interface Path {

export default class Diff<
TokenT,
ValueT extends Iterable<TokenT> = Iterable<TokenT>
ValueT extends Iterable<TokenT> = Iterable<TokenT>,
InputValueT = ValueT
> {
diff(
oldStr: ValueT,
newStr: ValueT,
oldStr: InputValueT,
newStr: InputValueT,
options: DiffCallbackNonabortable<ValueT>
): undefined;
diff(
oldStr: ValueT,
newStr: ValueT,
oldStr: InputValueT,
newStr: InputValueT,
options: AllDiffOptions & AbortableDiffOptions & CallbackOptionAbortable<ValueT>
): undefined
diff(
oldStr: ValueT,
newStr: ValueT,
oldStr: InputValueT,
newStr: InputValueT,
options: AllDiffOptions & CallbackOptionNonabortable<ValueT>
): undefined
diff(
oldStr: ValueT,
newStr: ValueT,
oldStr: InputValueT,
newStr: InputValueT,
options: AllDiffOptions & AbortableDiffOptions
): ChangeObject<ValueT>[] | undefined
diff(
oldStr: ValueT,
newStr: ValueT,
oldStr: InputValueT,
newStr: InputValueT,
options?: AllDiffOptions
): ChangeObject<ValueT>[]
diff(
oldString: ValueT,
newString: ValueT,
oldStr: InputValueT,
newStr: InputValueT,
// Type below is not accurate/complete - see above for full possibilities - but it compiles
options: DiffCallbackNonabortable<ValueT> | AllDiffOptions & Partial<CallbackOptionNonabortable<ValueT>> = {}
): ChangeObject<ValueT>[] | undefined {
Expand All @@ -64,8 +65,8 @@ export default class Diff<
callback = options.callback;
}
// Allow subclasses to massage the input prior to running
oldString = this.castInput(oldString, options);
newString = this.castInput(newString, options);
const oldString = this.castInput(oldStr, options);
const newString = this.castInput(newStr, options);

const oldTokens = this.removeEmpty(this.tokenize(oldString, options));
const newTokens = this.removeEmpty(this.tokenize(newString, options));
Expand Down Expand Up @@ -282,8 +283,8 @@ export default class Diff<
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
castInput(value: ValueT, options: AllDiffOptions): ValueT {
return value;
castInput(value: InputValueT, options: AllDiffOptions): ValueT {
return value as unknown as ValueT;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
26 changes: 13 additions & 13 deletions src/diff/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffJsonOptionsAbortable, DiffJsonOptionsNonabortable} from '../types.js';
import { tokenize } from './line.js';

class JsonDiff extends Diff<string, string> {
class JsonDiff extends Diff<string, string, string | object> {
get useLongestToken() {
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
Expand All @@ -11,7 +11,7 @@ class JsonDiff extends Diff<string, string> {

tokenize = tokenize;

castInput(value: string, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) {
castInput(value: string | object, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) {
const {undefinedReplacement, stringifyReplacer = (k, v) => typeof v === 'undefined' ? undefinedReplacement : v} = options;

return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), null, ' ');
Expand All @@ -31,31 +31,31 @@ export const jsonDiff = new JsonDiff();
* @returns a list of change objects.
*/
export function diffJson(
oldStr: string,
newStr: string,
oldStr: string | object,
newStr: string | object,
options: DiffCallbackNonabortable<string>
): undefined;
export function diffJson(
oldStr: string,
newStr: string,
oldStr: string | object,
newStr: string | object,
options: DiffJsonOptionsAbortable & CallbackOptionAbortable<string>
): undefined
export function diffJson(
oldStr: string,
newStr: string,
oldStr: string | object,
newStr: string | object,
options: DiffJsonOptionsNonabortable & CallbackOptionNonabortable<string>
): undefined
export function diffJson(
oldStr: string,
newStr: string,
oldStr: string | object,
newStr: string | object,
options: DiffJsonOptionsAbortable
): ChangeObject<string>[] | undefined
export function diffJson(
oldStr: string,
newStr: string,
oldStr: string | object,
newStr: string | object,
options?: DiffJsonOptionsNonabortable
): ChangeObject<string>[]
export function diffJson(oldStr: string, newStr: string, options?: any): undefined | ChangeObject<string>[] {
export function diffJson(oldStr: string | object, newStr: string | object, options?: any): undefined | ChangeObject<string>[] {
return jsonDiff.diff(oldStr, newStr, options);
}

Expand Down