Search code examples
typescriptamd

Referencing external module in typescript - error TS2304: Cannot find name 'general'


I prepared these two files:

1st one is general.d.ts file

interface IgeneralStatic {
    General: {
        Langs: any;
    };
}

declare var general: IgeneralStatic;

declare module 'general' {
    export = general;
}

2nd one is just something.ts file where i'm trying to import a file alias:

/// <reference path="general.d.ts" />

import general = require('general');

export class SpecificLangs extends general.General.Langs
  ...
}

When i trying to compile it i'm getting this error:

error TS2304: Cannot find name 'general'

The reason of this that I don't know where is my module and I can't import it for typescript usage. It is resolved on requirejs side as package. So there is no way to reference, using import, original general.ts file.


Solution

  • Your definition works, in that you can import it...

    For example, I can reference Langs like so:

    var x = general.General.Langs;
    

    However, you haven't declared that Langs is a class, so you can't extend it as if it were one.

    If it is implemented in a way that allows you to extend it, declaring it as a class will allow it to be used as a base class (simplified example to demonstrate that using a class works):

    declare module 'general' {
        export module General {
            export class Langs {
    
    
            }
        }
    }