4

I am trying to utilize type definition file https://www.npmjs.com/package/diff-match-patch as described in the link (Angularjs).

Please Note: I am using Visual Studio as IDE

var DiffMatchPatch = require('diff-match-patch'); <- Error here
var dmp = new DiffMatchPatch();
app.constant("dmp",dmp);

However, I am getting this error:

Cannot find name 'require'

This is how the type definition file is structured:

declare module "diff-match-patch" {
    type Diff = [number, string];

    export class diff_match_patch {
        static new (): diff_match_patch;

        Diff_Timeout: number;
        Diff_EditCost: number;
        Match_Threshold: number;
        Match_Distance: number;
        Patch_DeleteThreshold: number;
        Patch_Margin: number;
        Match_MaxBits: number;

        diff_main(text1: string, text2: string, opt_checklines?: boolean, opt_deadline?: number): Diff[];
        diff_commonPrefix(text1: string, text2: string): number;
        diff_commonSuffix(text1: string, text2: string): number;
        diff_cleanupSemantic(diffs: Diff[]): void;
        diff_cleanupSemanticLossless(diffs: Diff[]): void;
        diff_cleanupEfficiency(diffs: Diff[]): void;
        diff_cleanupMerge(diffs: Diff[]): void;
        diff_xIndex(diffs: Diff[], loc: number): number;
        diff_prettyHtml(diffs: Diff[]): string;
        diff_text1(diffs: Diff[]): string;
        diff_text2(diffs: Diff[]): string;
        diff_levenshtein(diffs: Diff[]): number;
        diff_toDelta(diffs: Diff[]): string;
        diff_fromDelta(text1: string, delta: string): Diff[];
    }

    export var DIFF_DELETE: number;
    export var DIFF_INSERT: number;
    export var DIFF_EQUAL: number;
}
1

1 Answer 1

2

I am trying to utilize type definition file https://www.npmjs.com/package/diff-match-patch as described in the link (Angularjs).

If you are using the npm module file in front end, you need to use something like webpack and compile with --module commonjs.

Also use import/require instead of var/require. More : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to utilize the file for back-end WebApi Visual Studio. This type definition file is very different from what I have used for example in moment.js it has declare moment = moment.MomentStatic and then for acessing the file in app.ts I used app.constant("moment", moment); I have no idea how I can make this work, would appreciate if you can elaborate.
Moreover, all definition files I utilize currently are interface based while this one is class based.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.