Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 打字稿:';新';调用函数_Javascript_Typescript_New Operator - Fatal编程技术网

Javascript 打字稿:';新';调用函数

Javascript 打字稿:';新';调用函数,javascript,typescript,new-operator,Javascript,Typescript,New Operator,最近我想把我的一个附带项目转换成打字稿。但是我在使用new调用函数时遇到了麻烦 我试图调用从另一个文件导入的函数,如下所示: //file.js中的函数 函数Foo(){ 这个.x=1; 这个y=2; } Foo.prototype.set=函数(){ 返回此.x+此.y; }; 出口{Foo}; //另一个文件中调用Foo的函数 从“./文件”导入{Foo}; 函数getSum(){ 让foo=new foo();//我在这里得到了下面的错误!!! foo.set(); } 当我尝试键入此

最近我想把我的一个附带项目转换成打字稿。但是我在使用new调用函数时遇到了麻烦

我试图调用从另一个文件导入的函数,如下所示:

//file.js中的函数
函数Foo(){
这个.x=1;
这个y=2;
}
Foo.prototype.set=函数(){
返回此.x+此.y;
};
出口{Foo};
//另一个文件中调用Foo的函数
从“./文件”导入{Foo};
函数getSum(){
让foo=new foo();//我在这里得到了下面的错误!!!
foo.set();
}
当我尝试键入此内容时,会出现以下错误:
其目标缺少构造签名的“new”表达式隐式具有“any”类型。


通过查看,我了解电话签名应写如下:

type SomeConstructor={
新(s:string):SomeObject;
};
函数fn(ctor:SomeConstructor){
返回新ctor(“你好”);
}

但我不知道如何将上述类型应用于我的“Foo”函数。 我试图将构造签名应用于函数,但无法正确放置

//将“file.js”中的函数-->重命名为“file.tsx”
类型FooType={
x:号码,,
y:号码,
};
类型构造函数={
new():FooType
};
函数Foo(this:FooType){//如何将FooConstructor添加到此函数?
这个.x=1;
这个y=2;
}
Foo.prototype.set=函数():编号{
返回此.x+此.y;
};
在导出/导入或函数调用期间,我无法应用它。下面的所有错误都会抛出

export { Foo: FooConstructor };
import { Foo: FooConstructor } from './file';
let foo = new Foo() as FooConstructor;
那么我应该把Foo函数改成类吗?这是唯一可能的输入方法吗?!我看到许多博客展示了如何键入类。但即使使用这种方法,我也会遇到一个错误,即,
类型“FooType”不能分配给类型“FooConstructor”

我在这里迷路了。感谢您的帮助


编辑:My File.ts现在看起来像这样:

我在File.ts文件中添加声明,如下所示:

类型FooType={
x:号码,,
y:号码,
};
声明类Foo{
构造函数();
x:数字;
y:数字;
setHeader():编号;
}
函数Foo(this:FooType){
这个.x=1;
这个y=2;
}
Foo.prototype.set=函数():编号{
返回此.x+此.y;
};
出口{Foo};
```

在.d.ts文件中或直接在.ts文件中:

declare class Foo {
    public x: number;
    public y: number;

    set(): number;
}

解决此问题的唯一方法是将以下函数转换为类:

function Foo(this: FooType) { // How do I add FooConstructor to this?
  this.x = 1;
  this.y = 2;
}
Foo.prototype.set = function(): number {
   return this.x + this.y;
};
致:


将相应命名的声明文件(在本例中名为
file.d.ts
)与
file.js
放在一起,并使用以下内容
export declare class Foo{constructor(x:number,y:number);set():number;}
稍微调整@AluanHaddad的声明,因为它似乎需要一个零参数构造函数和公共属性
x
y
声明类Foo{constructor();set():number;x:number;y:number;}
我尝试在文件
file.d.ts
中添加上述声明。但是我仍然得到
“new”表达式,它的目标缺少构造签名,在我需要调用Foo的文件中隐式地有一个“any”类型!这里的答案需要一个构造函数,正如@Aluan Haddad在上面所评论的那样!我认为它不需要构造函数。上面的声明已经暗示了一个没有参数的隐式构造函数。将
Foo
悬停在操场的
new
之后,您将看到。不过,最好添加一个构造函数,以明确该构造函数不接受任何参数。@Amitraravi不,我不会包含构造函数。我最初这么做的唯一原因是因为我没有读到问题,认为它需要参数。这个答案中唯一缺少的是
export
keywordOK,所以我尝试通过添加export和删除构造函数来运行它。但是它抛出了这个错误:
TypeError:replicate声明“Foo”
now。即使包含构造函数,它也会抛出此错误。也许TS引擎会解析实现
函数Foo()
的文件?你能展示一下你的文件结构吗?请更好地描述一下你的答案。你的答案是什么
class Foo() {
  x: number;
  y: number;
  constructor() {
    this.x = 1;
    this.y = 2;
  }
  set (): number {
   return this.x + this.y;
  }
}
>Adjunto mi código espero ayude
>Para ejecutar solo realice new Functions() 
>para llamar a la clase internamente se ejecutar
> el constructor de Functions

type SomeConstructor = {
  new (s:string):SomeObject;
}


type SomeObject = any;


export class Example{
   constructor(s:string){
     console.log(s)
   }
}


 export class Functions {
    constructor(){
      const callable = (someObject:SomeObject)=>{
        return someObject;
      }

      this.fnSomeConstructor(callable(new Example("Hola")));
    }

   fnSomeConstructor(ctor: SomeConstructor) {
    return new func(func.name);
   }
 }