Skip to content

Question about importing js file in typescript fashion #2712

@ericlu88

Description

@ericlu88

I am trying to import an existing javascript source file(not a 3rd party module) into my typescript file using import ... = require() syntax with no success. Could you please help me point out anything I did wrong in the following isolated example?

add.js

module.exports = function (n1, n2) {
    return n1 + n2;
};

add.d.ts

declare module "add" {
    function add(n1: number, n2: number): number;
    export = add;
}

main.ts

/// <reference path='node.d.ts' />
/// <reference path='add.d.ts' />
import add = require('./add');
console.log(add(1, 2));

When I try to compile main.ts, I got the following error:

error TS2306: File 'add.d.ts' is not an external module.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions