[英]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.