[英]Constructor error using namespace in Typescript
I'm trying to use constructors inside a class using namespace, but every time I want to push an object into array, i get an typerror, this doesn't happen when I'm not using namespace 我试图在使用命名空间的类中使用构造函数,但是每次我想将一个对象推入数组时,都会出现输入错误,当我不使用命名空间时不会发生这种情况
why i'm getting this error just for use namespace? 为什么我只为使用命名空间而收到此错误?
these are my test classes 这些是我的测试课
1.ts 1.ts
namespace pruebas {
export class User {
private _name: string;
private _ape: string;
constructor(name,ape){
this._name = name;
this._ape = ape;
}
get name(): string {
return this._name;
}
set name(value: string) {
this._name = value;
}
get ape(): string {
return this._ape;
}
set ape(value: string) {
this._ape = value;
}
}
}
2.ts 2.ts
///<reference path="1.ts"/>
namespace pruebas {
import pr = pruebas.User;
let us = new User(`saresease`, `ssfse`);
let vw = new User(`ghrebbre`, `bnerev`);
let r =[]
r.push(us,vw);
console.log(r)
}
This is the error 这是错误
var us = new pruebas.User("saresease", "ssfse");
^
TypeError: pruebas.User is not a constructor
at pruebas (C:\Users\Downloads\ts project\src\testnames\2.js:4:14)
at Object.<anonymous> (C:\Users\Downloads\ts project\src\testnames\2.js:9:3)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
You need to compile with --outFile
for this to work. 您需要使用
--outFile
进行编译才能正常工作。
More generally, since you're running a node process, you shouldn't be putting things into the global namespace, as this won't really work for long (as soon as you need to import
some other thing, this all falls apart). 更一般而言,由于您正在运行节点进程,因此不应将其放到全局名称空间中,因为这实际上不会持续很长时间(一旦需要
import
其他内容,所有这些都将分崩离析) 。 Use top-level export
and import
declarations to share components between files. 使用顶级
export
和import
声明在文件之间共享组件。
See also How do I use namespaces with TypeScript external modules? 另请参阅如何将名称空间与TypeScript外部模块一起使用?
1.export the namespace in 1.ts . 1.在1.ts中导出名称空间。
export = pruebas;
2.import the namespace using import=require(..) in 2.ts . 2.使用2.ts中的 import = require(..)导入名称空间。
import pruebas=require("./1.ts");
you must compile 2.ts & 1.ts into a single script file.if you define 1.ts as both global library and a module,you must specify a module loader via option --module=amd / --module=system when using
tsc
compile .ts into a single .js file.您必须将2.ts和1.ts编译到一个脚本文件中。如果将1.ts定义为全局库和模块,则必须在以下情况下通过--module = amd / --module = system选项指定模块加载器使用
tsc
.ts编译为单个.js文件。
using triple-slash directive to define your dependency in 2.ts . 使用三斜杠指令在2.ts中定义依赖项。
///<reference path="./1.ts"/>;
compile the 2.ts with tsc --outFile 2.js <srcRoot>/2.ts
,then add the js in browser you can see the result.if you want to see details, let's go!!! 用
tsc --outFile 2.js <srcRoot>/2.ts
编译2.ts,然后在浏览器中添加js即可看到结果。如果想查看详细信息, 那就开始吧!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.