打字稿:'new' 函数调用
typescript: 'new' call to function
最近我想把我的一个副项目转换成 Typescript。但是我在使用 new.
调用函数时遇到了问题
我正在尝试调用从另一个文件导入的函数,如下所示:
// Function in 'file.js'
function Foo() {
this.x = 1;
this.y = 2;
}
Foo.prototype.set = function() {
return this.x + this.y;
};
export { Foo };
// Function in another file calling Foo
import { Foo } from './file';
function getSum() {
let foo = new Foo(); // I got the below error here!!!
foo.set();
}
当我尝试键入此内容时,出现此错误:'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
。
看了typescript documentation,我理解调用签名应该这样写:
type SomeConstructor = {
new (s: string): SomeObject;
};
function fn(ctor: SomeConstructor) {
return new ctor("hello");
}
但我不知道如何将上述类型应用到我的 'Foo' 函数中。
我尝试将构造签名应用于函数,但无法正确放置。
// Function in 'file.js' --> renamed to 'file.tsx'
type FooType = {
x: number,
y: number,
};
type FooConstructor = {
new (): FooType
};
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;
};
我无法在导出/导入或函数调用期间应用它。以下所有抛出错误。
export { Foo: FooConstructor };
import { Foo: FooConstructor } from './file';
let foo = new Foo() as FooConstructor;
所以我是否必须将 Foo 函数更改为 class,这是唯一可能的输入方式吗?!我看到很多博客展示了如何输入 class。但即使采用这种方法,我还是收到一条错误消息,说 Type 'FooType' is not assignable to type 'FooConstructor'
.
我在这里迷路了。感谢您的帮助!
编辑:我的 File.ts 现在看起来像这样:
我在File.ts文件中添加声明,像这样:
type FooType = {
x: number,
y: number,
};
declare class Foo {
constructor();
x: number;
y: number;
setHeader(): number;
}
function Foo(this: FooType) {
this.x = 1;
this.y = 2;
}
Foo.prototype.set = function(): number {
return this.x + this.y;
};
export { Foo };
```
在 .d.ts 文件中或直接在 .ts 文件中:
declare class Foo {
public x: number;
public y: number;
set(): number;
}
解决这种情况的唯一方法是将以下函数转换为 class:
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;
};
至:
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);
}
}
最近我想把我的一个副项目转换成 Typescript。但是我在使用 new.
调用函数时遇到了问题我正在尝试调用从另一个文件导入的函数,如下所示:
// Function in 'file.js'
function Foo() {
this.x = 1;
this.y = 2;
}
Foo.prototype.set = function() {
return this.x + this.y;
};
export { Foo };
// Function in another file calling Foo
import { Foo } from './file';
function getSum() {
let foo = new Foo(); // I got the below error here!!!
foo.set();
}
当我尝试键入此内容时,出现此错误:'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
。
看了typescript documentation,我理解调用签名应该这样写:
type SomeConstructor = {
new (s: string): SomeObject;
};
function fn(ctor: SomeConstructor) {
return new ctor("hello");
}
但我不知道如何将上述类型应用到我的 'Foo' 函数中。 我尝试将构造签名应用于函数,但无法正确放置。
// Function in 'file.js' --> renamed to 'file.tsx'
type FooType = {
x: number,
y: number,
};
type FooConstructor = {
new (): FooType
};
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;
};
我无法在导出/导入或函数调用期间应用它。以下所有抛出错误。
export { Foo: FooConstructor };
import { Foo: FooConstructor } from './file';
let foo = new Foo() as FooConstructor;
所以我是否必须将 Foo 函数更改为 class,这是唯一可能的输入方式吗?!我看到很多博客展示了如何输入 class。但即使采用这种方法,我还是收到一条错误消息,说 Type 'FooType' is not assignable to type 'FooConstructor'
.
我在这里迷路了。感谢您的帮助!
编辑:我的 File.ts 现在看起来像这样:
我在File.ts文件中添加声明,像这样:
type FooType = {
x: number,
y: number,
};
declare class Foo {
constructor();
x: number;
y: number;
setHeader(): number;
}
function Foo(this: FooType) {
this.x = 1;
this.y = 2;
}
Foo.prototype.set = function(): number {
return this.x + this.y;
};
export { Foo };
```
在 .d.ts 文件中或直接在 .ts 文件中:
declare class Foo {
public x: number;
public y: number;
set(): number;
}
解决这种情况的唯一方法是将以下函数转换为 class:
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;
};
至:
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);
}
}