简体   繁体   中英

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?

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 = {
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM