JeeShen Lee
JeeShen Lee

Reputation: 3816

Typescript Reference to external Module in Namespace

I'm trying to refer to an external module 'moment' in my namespace 'company.controllers'

namespace company.controllers {    
    import { moment } from 'moment';

    'use strict'

    export class WidgetAdminDailyStatisticController extends Controller<company.data.AdminDailyStatisticViewModel> {
        customRepository: company.data.CustomRepository;
        searchDate: Date;

        static $inject: Array<string> = [
            'CustomRepository'
        ];
        constructor(
            customRepository: company.data.CustomRepository
        ) {
            super();

            this.customRepository = customRepository;
        }

        initialize(): void {
            this.searchDate = new Date();

            //let a: moment = new moment();


        }
    }
}

I'm getting "Import declarations in a namespace cannot reference to a module"

How do I import moment and use in my typescript?

Upvotes: 1

Views: 1401

Answers (1)

Paleo
Paleo

Reputation: 23742

You can import it outside the namespace:

import { moment } from 'moment';

namespace company.controllers {
    // ...
}

... but you can't import a module in non-module scripts. If your namespace is not in a module, it's not possible. The solution is to use moment as a global variable, and to find (or write) another definition file for that global variable.

Upvotes: 0

Related Questions