繁体   English   中英

如何解决错误“此表达式不可构造”?

[英]How can I solve error “This Expression is not constructable”?

我想将js class 导入ts。 但是我得到了错误This expression is not constructable.
和 typescript 编译器说 A 没有构造函数签名。
我该如何解决这个问题?

索引.ts

import A from "aaa";
const a = new A(); //error: this expression is not constructable.  
  
/*
and result of console.log(A); is [Function: A].

result of console.log(A.toString()); is below
class A {
  constructor(name) { this.name = name; }
}
*/

aaa 模块中的 index.js。

class A {
  constructor(name) { this.name = name; }
}
module.exports = A;

aaa 模块中的 index.d.ts。

export declare class A {
  constructor(name:string);

  name:string;
} 

我可以用下面的代码在js中构造A。 但不能在ts。

const A = require("aaa");
const a = new A();

请参阅下面的评论。 原创

// Note that ES6 modules cannot directly export class objects.
// This file should be imported using the CommonJS-style:
//   import x = require('[~THE MODULE~]');
//
// Alternatively, if --allowSyntheticDefaultImports or
// --esModuleInterop is turned on, this file can also be
// imported as a default import:
//   import x from '[~THE MODULE~]';
//
// Refer to the TypeScript documentation at
// https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
// to understand common workarounds for this limitation of ES6 modules.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM