Typescript: cannot find name 'require'

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

Please note: I am using Visual Studio as an IDE

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

However, I get this error:

Cannot find name 'require'

Here's 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;
}
+4
source share
1 answer

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

If you are using the npm module file in the interface, you need to use something like webpackand compile with --module commonjs.

import/require var/require. : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

+2

Source: https://habr.com/ru/post/1613560/


All Articles