Search code examples
angularnoty

angular 5 noty and AOT


I'm using noty (https://ned.im/noty) with angular 5.2.9 this way:

import { Injectable } from '@angular/core';
import Noty = require('noty');

@Injectable()
export class NotificationService {
    private noty(options) {
        return new Noty(options).show();
    }
    ...
}

and works fine.

but when I'm building with AOT (npm run build:aot) I get:

ERROR in src/app/sys/util/notification.service.ts(2,1): error TS1202:  
Import assignment cannot be used when targeting ECMAScript modules.  
Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

It's a project based on https://github.com/gdi2290/angular-starter.
When using import Noty from 'noty'; the AOT build is fine but when running npm run server I get ERROR TypeError: noty_1.default is not a constructor.

node_modules/noty/index.d.ts starts with:

declare module 'noty' {
    export = Noty;
}

declare class Noty {
    constructor(options?: Noty.Options);
...

How should I solve this?


Solution

  • I found that is working this way:

    /// <reference path="../../../../node_modules/noty/index.d.ts" />
    
    import { Injectable } from '@angular/core';
    import Noty = require('noty');
    
    @Injectable()
    export class NotificationService {
        private noty(options) {
            return new Noty(options).show();
        }
        ...
    }
    

    Because noty does not have the d.ts placed into node_modules/@types/... then I guess I have to manually reference it.

    PS: also set "no-reference": false rule in tsling.json otherwise will mark the reference as an error