BeetleJuice
BeetleJuice

Reputation: 40896

Typescript declaration: why does this throw an error

I'm just learning Typescript. I'm writing an Angular 2 app with typescript 2, using SystemJS 0.15.31 as my module loader.

I'd like to define app-wide constants. in one file, and import it in my components as needed. This is what I have:

root/systemjs.config.js

System.config({
    map:{
        'app': 'dist',
        'config': 'dist/config',
        ...
    }
});

root/dev/config.ts

export module Config {
    var version:string = '1';
}

root/dev/app/app.component.ts

import {Config} from 'config';
...
export class AppComponent {
    ...
    version = Config.version;
}

After transpilation to Javascript, the .ts files are put in dist/. However, the typescript compiler shows this error on the first line of app.component.ts and in the browser, AppComponent doesn't see Config.version

error TS2307: Cannot find module 'config'.

What's the problem with my syntax?

Upvotes: 1

Views: 79

Answers (1)

santi-elus
santi-elus

Reputation: 310

I think your problem is that SystemJS is expecting dist/config to be a folder.

Add an extension to the file path, like so:

map: {
    ...
    'config': 'dist/config.js' 
    ...
}

PS: export module is not typescript. I think you want:

export const Config = {
   ...
}

Upvotes: 1

Related Questions