Mathias Dolidon
Mathias Dolidon

Reputation: 3903

Failing to recognize an internal module when written in a referenced file

Typescript newbie, first project. I carefully read the handbook, but fail to solve a simple problem. I have an internal module :

module MDVault1Encoding {
    export function encode(store:Store, pwHash: Buffer): string {
        (...)
        return (...);
    }

    export function decode(encoded:string, pwHash: Buffer): Store {
        (...)
        return (...);
    } 
}

Then I have client code outside this module that calls these functions, like in an instance method :

this.store = MDVault1Encoding.decode(container.data, this.pwHash);

When the module definition and the client code live in the same .ts file, all goes well.

But when I put the module into an md_vault_1_encoding.ts file, and the client code in fileio.ts, adding a /// <reference path="./md_vault_1_encoding.ts" /> in the latter's header, things go wrong.

md_vault_1_encoding.ts compiles neatly, but fileio.ts yields this :

$ tsc fileio.ts
fileio.ts(39,26): error TS2304: Cannot find name 'MDVault1Encoding'.
fileio.ts(48,19): error TS2304: Cannot find name 'MDVault1Encoding'.

What am I doing wrong ?

Upvotes: 0

Views: 36

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220964

Somewhere else in md_vault_1_encoding.ts you have a top-level import or export declaration.

This turns your file into a module (AKA "external module") and means your file doesn't put any properties in the global namespace.

You can either remove that import / export declaration (probably not possible), or you can add an export modifier to your namespace (export module MDVault1Encoding { ...) and then in fileio.ts write

import { MDVault1Encoding } from './md_vault_1_encoding';

Upvotes: 1

Related Questions