我对Typescript命名空间,模块有些困惑。我需要解决一个简单的任务-创建两个打字稿文件,并使用一个彼此对应的类。 但是,如何使它正常工作。
结构:
installation :
typescript :
Person.ts
main.ts
main.ts的代码是
//Using this tag I am trying to load another file
/// <reference path = "Person.ts" />
var person:Person = new Person("Sergey" , "Sckoriy");
console.log(person.greet());
Person.ts的代码是
export class Person {
name : string;
surname : string;
constructor (name : string , surname : string){
this.name = name;
this.surname = surname;
}
greet():string {
var text:string = "Hello" + this.name;
return text;
}
}
但是,当使用
编译时 ..../tsc main.ts
存在诸如以下错误:
main.ts:3:12 - error TS2304: Cannot find name 'Person'.
3 var person:Person = new Person("Sergey" , "Sckoriy");
main.ts:3:25 - error TS2552: Cannot find name 'Person'. Did you mean 'person'?
3 var person:Person = new Person("Sergey" , "Sckoriy");
main.ts:3:5
3 var person:Person = new Person("Sergey" , "Sckoriy");
~~~~~~
'person' is declared here.
Found 2 errors.
实际上,我已经引用了另一个带有标签的文件,但该文件不起作用...这是怎么回事?
答案 0 :(得分:0)
import { Person } from './Person