如何将继承的属性包括在TSC生成的.js文件的类型定义中?

发布于 2025-02-06 11:57:24 字数 1074 浏览 0 评论 0原文

我正在尝试通过.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2025-02-13 11:57:24

您忘了在childclass.jsprogram.js上导入其他文件。应该是这样的:


parentclass.js

/**
* This is the parent class.
*
* @class
*/
export class ParentClass {
  parentFunction(){
    console.log("Running parent function");
  }
}

childclass.js

import { ParentClass } from './ParentClass'

/**
* This is a child class.
*
* @class
* @extends {ParentClass}
*/
export class ChildClass extends ParentClass {
  // ChildClass vars and functions here
}

program.js

import { ChildClass } from './childClass'

let cc = new ChildClass();
cc.parentFunction();

You've forgotten to import the other files on the childClass.js and program.js. It should be something like this:


ParentClass.js

/**
* This is the parent class.
*
* @class
*/
export class ParentClass {
  parentFunction(){
    console.log("Running parent function");
  }
}

childClass.js

import { ParentClass } from './ParentClass'

/**
* This is a child class.
*
* @class
* @extends {ParentClass}
*/
export class ChildClass extends ParentClass {
  // ChildClass vars and functions here
}

program.js

import { ChildClass } from './childClass'

let cc = new ChildClass();
cc.parentFunction();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文