使用TypeScript进行默认导入

时间:2016-03-23 14:02:56

标签: module typescript ecmascript-6

使用tsc 1.8.9 ......为什么这些导入不起作用?我以为TypeScript实现了ES6模块语法?

"类/ person.ts"

export default class Person {
    protected _name: string;
    protected _language: string;

    constructor(name: string) {
        this._name = name;

        this.hello();
    }

    public hello() {
        console.log("Hello, " + this._name);
        console.log("Lang: " + this._language);
    }
}

"类/ englishman.ts"

import Person from "person"

export default class Englishman extends Person {
    constructor(name: string){
        this._language = "en_GB";

        super(name);
    }
}

" main.ts"

import * as $ from "jquery";
import Englishman from "classes/englishman";

let tom: Person = new Englishman("Tom");
console.log(tom);


$("body").html(`<h1>TEST</h1>`);

错误:

  

source / main.ts(2,24):错误TS2307:找不到模块   &#39;类/英国人&#39 ;. source / main.ts(4,10):错误TS2304:找不到   姓名&#39;人物&#39;。 [13时53分43秒]   TypeScript:2个语义错误

2 个答案:

答案 0 :(得分:0)

确保您正在使用ES6目标进行编译,如果我没有弄错,ES5仍然是默认目标。

答案 1 :(得分:0)

经过一些更改,它对我有用,在ES5和ES6中进行了测试。我希望能帮到你:

原始

  1. (unsigned int) A = B; // This time assignment is intended
  2. import Person from "classes/person";
  3. 更改测试

    1. import Englishman from "classes/englishman";
    2. import Person from './person';
    3. 也许您需要检查目录树。

      添加

      1. import Englishman from './classes/englishman';
      2. import Person       from './classes/person';
        import Person from './person'; //<-- change
        
        export default class Englishman extends Person {
            constructor(name: string){
                this._language = "en_GB";
        
                super(name);
            }
        }
        
         export default class Person {
                protected _name: string;
                protected _language: string;
        
                constructor(name: string) {
                    this._name = name;
        
                    this.hello();
                }
        
                public hello() {
                    console.log("Hello, " + this._name);
                    console.log("Lang: " + this._language);
                }
            }
        
        import Englishman   from './classes/englishman'; //<-- change
        import Person       from './classes/person';     //<-- add
        
        class HelloWorld{
            public static main(){      
        
                let tom: Person = new Englishman("Tom");
                console.log(tom);
        
            }
        }
        HelloWorld.main();
        
        • tsc Version 1.8.2
        • node v5.4.1