diff --git a/types/diff/diff-tests.ts b/types/diff/diff-tests.ts index 6cb70ec84d4f533..5e222029ae1d7f9 100644 --- a/types/diff/diff-tests.ts +++ b/types/diff/diff-tests.ts @@ -25,6 +25,12 @@ Diff.diffLines( oneChangePerToken: true, }, ); +// $ExpectType void +Diff.createPatch('filename', 'A', 'a', undefined, undefined, { + callback: (value) => { + value; // $ExpectType string + }, +}) const diffArraysResult = Diff.diffArrays(["a", "b", "c"], ["a", "c", "d"]); diffArraysResult.forEach(result => { diff --git a/types/diff/index.d.ts b/types/diff/index.d.ts index 1944a1dae5f4904..cc639d000dd01b6 100644 --- a/types/diff/index.d.ts +++ b/types/diff/index.d.ts @@ -1,12 +1,12 @@ export as namespace Diff; -export type Callback = (value: Change[]) => void; +export type Callback = (value: T) => void; -export interface CallbackOptions { +export interface CallbackOptions { /** - * Callback to call with the result instead of returning the result directly. + * if provided, the diff will be computed in async mode to avoid blocking the event loop while the diff is calculated. The value of the `callback` option should be a function and will be passed the computed diff or patch as its first argument. */ - callback: Callback; + callback: Callback; } export interface BaseOptions { @@ -161,7 +161,7 @@ export class Diff { diff( oldString: string, newString: string, - options?: Callback | (ArrayOptions & Partial), + options?: Callback | (ArrayOptions & Partial>), ): Change[]; pushComponent(components: Change[], added: boolean, removed: boolean): void; @@ -195,7 +195,7 @@ export function diffChars(oldStr: string, newStr: string, options?: BaseOptions) export function diffChars( oldStr: string, newStr: string, - options: Callback | (BaseOptions & CallbackOptions), + options: Callback | (BaseOptions & CallbackOptions), ): void; /** @@ -207,7 +207,7 @@ export function diffWords(oldStr: string, newStr: string, options?: WordsOptions export function diffWords( oldStr: string, newStr: string, - options: Callback | (WordsOptions & CallbackOptions), + options: Callback | (WordsOptions & CallbackOptions), ): void; /** @@ -223,7 +223,7 @@ export function diffWordsWithSpace( export function diffWordsWithSpace( oldStr: string, newStr: string, - options: Callback | (WordsOptions & CallbackOptions), + options: Callback | (WordsOptions & CallbackOptions), ): void; /** @@ -235,7 +235,7 @@ export function diffLines(oldStr: string, newStr: string, options?: LinesOptions export function diffLines( oldStr: string, newStr: string, - options: Callback | (LinesOptions & CallbackOptions), + options: Callback | (LinesOptions & CallbackOptions), ): void; /** @@ -247,7 +247,7 @@ export function diffTrimmedLines(oldStr: string, newStr: string, options?: Lines export function diffTrimmedLines( oldStr: string, newStr: string, - options: Callback | (LinesOptions & CallbackOptions), + options: Callback | (LinesOptions & CallbackOptions), ): void; /** @@ -259,7 +259,7 @@ export function diffSentences(oldStr: string, newStr: string, options?: BaseOpti export function diffSentences( oldStr: string, newStr: string, - options: Callback | (BaseOptions & CallbackOptions), + options: Callback | (BaseOptions & CallbackOptions), ): void; /** @@ -271,7 +271,7 @@ export function diffCss(oldStr: string, newStr: string, options?: BaseOptions): export function diffCss( oldStr: string, newStr: string, - options: Callback | (BaseOptions & CallbackOptions), + options: Callback | (BaseOptions & CallbackOptions), ): void; /** @@ -288,7 +288,7 @@ export function diffJson( export function diffJson( oldObj: string | object, newObj: string | object, - options: Callback | (JsonOptions & CallbackOptions), + options: Callback | (JsonOptions & CallbackOptions), ): void; /** @@ -321,6 +321,15 @@ export function createTwoFilesPatch( newHeader?: string, options?: PatchOptions, ): string; +export function createTwoFilesPatch( + oldFileName: string, + newFileName: string, + oldStr: string, + newStr: string, + oldHeader?: string, + newHeader?: string, + options?: PatchOptions & CallbackOptions, +): void; /** * Creates a unified diff patch. @@ -340,6 +349,14 @@ export function createPatch( newHeader?: string, options?: PatchOptions, ): string; +export function createPatch( + fileName: string, + oldStr: string, + newStr: string, + oldHeader?: string, + newHeader?: string, + options?: PatchOptions & CallbackOptions, +): void; /** * This method is similar to `createTwoFilesPatch()`, but returns a data structure suitable for further processing. @@ -362,6 +379,15 @@ export function structuredPatch( newHeader?: string, options?: PatchOptions, ): ParsedDiff; +export function structuredPatch( + oldFileName: string, + newFileName: string, + oldStr: string, + newStr: string, + oldHeader?: string, + newHeader?: string, + options?: PatchOptions & CallbackOptions, +): void; /** * Applies a unified diff patch.