简体   繁体   中英

Error when mixing javascript and typescript in angular

I am trying to use typescript in part of my angular application. I have a ts file describing a class User:

module App.Tools {
    export class User {
        name: string;
        constructor(name: string) {
            this.name = name;
        }
    }
}

And in my JS file (es2015) I try to import the class User:

import  {User} from 'User.ts';

export default class AuthFactory {
    /**
     * @param  {[type]}
     * @return {[type]}
     */
    constructor($firebaseAuth) {
        this.ref = new Firebase("...");

        // create an instance of the authentication service
        this.auth = $firebaseAuth(this.ref);

        var authData = this.auth.$getAuth();

        if (authData) {
            this.currentUser = new User(authData.google.displayName, authData.provider);
        } else {
            this.currentUser = null;
        }
    }
}

This doesn't work as I get an error : "User is not defined".

I am using gulp with browserify, tsify and babel.

I think you need an " external " typescript module in "User.ts" file:

class User {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
}

export = User;

Update 1

Using internal modules:

module App.Tools {
    export class User {
        name: string;
        constructor(name: string) {
            this.name = name;
        }
    }
}

export = App.Tools;

To use this in import:

import { User } from 'User.ts';

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