如何将继承的属性包括在TSC生成的.js文件的类型定义中?
我正在尝试通过.D.TS文件将TypeScript支持添加到 vanilla javascript 库。我试图使用“ TSC”命令生成这些类型定义文件,但是我遇到了许多“属性'propertyName'”错误。
这是在仅在其父类代码中定义该函数时试图在子类实例上调用函数的行。看来,尽管JSDOC能够通过在儿童类的JSDOC评论中使用“ @Extends”来推断继承的功能,但TSC不是。
例如:
parentclass.js
/**
* This is the parent class.
*
* @class
*/
export class ParentClass {
parentFunction(){
console.log("Running parent function");
}
}
childclass.js
/**
* This is a child class.
*
* @class
* @extends {ParentClass}
*/
import {ParentClass} from "./ParentClass";
class ChildClass extends ParentClass {
// ChildClass vars and functions here
}
program.ts
import {ChildClass} from "./ChildClass";
let cc = new ChildClass();
cc.parentFunction();
尝试编译代码后,TSC会丢弃此错误:
错误ts2339:属性“ parentfunction”不存在 'ChildClass'。
有什么办法可以解决此问题?如果没有办法使打字稿编译器识别继承的属性,是否有某种方法可以使TSC忽略此问题,因此它至少可以完成生成类型的定义文件,并允许我手动将丢失的属性添加到相应的儿童类类型中定义?
I am trying to add typescript support to a vanilla javascript library via .d.ts files. I am trying to generate these type definition files with the "tsc" command, but I am running into many "property 'propertyName' does not exist on type 'typeName'" errors.
This is happening on lines that attempt to call a function on an instance of a child class when that function is defined only in the code for its parent class. It seems that while JSDoc is capable of inferring inherited functions by using "@extends" in the JSDoc comments of a child class, tsc is not.
For example:
ParentClass.js
/**
* This is the parent class.
*
* @class
*/
export class ParentClass {
parentFunction(){
console.log("Running parent function");
}
}
childClass.js
/**
* This is a child class.
*
* @class
* @extends {ParentClass}
*/
import {ParentClass} from "./ParentClass";
class ChildClass extends ParentClass {
// ChildClass vars and functions here
}
program.ts
import {ChildClass} from "./ChildClass";
let cc = new ChildClass();
cc.parentFunction();
Upon attempting to compile the code, tsc will throw this error:
error TS2339: Property 'parentFunction' does not exist on type
'ChildClass'.
Is there any way to resolve this issue? If there is no way to make the typescript compiler recognize inherited properties, is there some way to make tsc ignore this issue so it can at least finish generating the type definition files and allow me to manually add the missing properties to the corresponding child class type definitions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘了在
childclass.js
和program.js
上导入其他文件。应该是这样的:parentclass.js
childclass.js
program.js
You've forgotten to import the other files on the
childClass.js
andprogram.js
. It should be something like this:ParentClass.js
childClass.js
program.js