简体   繁体   English

在Typescript中使用名称空间的构造函数错误

[英]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. 使用顶级exportimport声明在文件之间共享组件。

See also How do I use namespaces with TypeScript external modules? 另请参阅如何将名称空间与TypeScript外部模块一起使用?

you can use namespace in a module after you export it. 您可以在导出模块后在模块中使用名称空间。

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 can use namespace as global library. 您可以将名称空间用作全局库。

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.ts1.ts编译到一个脚本文件中。如果将1.ts定义为全局库和模块,则必须在以下情况下通过--module = amd / --module = system选项指定模块加载器使用tsc .ts编译为单个.js文件。

  1. using triple-slash directive to define your dependency in 2.ts . 使用三斜杠指令在2.ts中定义依赖项。

     ///<reference path="./1.ts"/>; 
  2. 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.

相关问题 打字稿,使用没有构造函数的类 - Typescript, using classes without constructor 为 React Typescript 导入包时不是构造函数错误 - Not a constructor error when importing packages for React Typescript React中的Typescript:状态不会放置在构造函数中会导致错误 - Typescript in React: State will not be placed in the constructor will cause an error 继承和 TypeScript 错误:X 不是构造函数类型 - Error with Inheritance and TypeScript: X is not a constructor function type 关于 class 构造函数中未初始化变量的 TypeScript 错误 - TypeScript error regarding uninitialized variables in class constructor TypeScript - 使用构造函数属性创建实例 - TypeScript - Create an instance using the constructor property JavaScript使用构造函数模式创建的对象避免全局命名空间污染 - JavaScript Avoiding the global namespace pollution by objects created using constructor pattern 在Angular2 / TypeScript中找不到模型的名称空间错误 - Cannot find namespace error for model in Angular2/TypeScript Typescript - 将命名空间导入另一个命名空间 - Typescript - Import Namespace Into Another Namespace 打字稿错误:'RRule' 仅引用类型,但在此处用作命名空间 - typescript error: 'RRule' only refers to a type, but is being used as a namespace here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM