TS错误求助:类型'Prompto'不存在属性'components'
修复方案
问题原因
你在.d.ts中声明了Prompto类的components属性,但这个声明仅作为类型提示存在,没有实际的属性定义逻辑。当你继承这个类时,TypeScript会检查子类是否实际拥有该属性,而当前实现类里未显式定义components,因此触发报错。
具体修复步骤
在你的实现类Prompto中,显式添加components属性的定义:
import PromptoOptions = prompto.PromptoOptions; import { run } from "../functions/run"; import QOptions = prompto.QOptions; export class Prompto extends prompto.Prompto { // 显式定义components属性,匹配.d.ts中的类型声明 public components: QOptions[]; constructor(options: PromptoOptions = { showPreview: false, outputPath: "./", fileName: "output", components: [] }) { super(); this.showPreview = options.showPreview this.fileName = options.fileName this.outputPath = options.outputPath this.components = []; } addComponents(arr: Array<QOptions>) { for (const item of arr) { if (!item.name || !item.type) { throw new TypeError("Invalid item") } this.components.push(item) } } run() { console.log(this) run({ components: this.components }) } }
额外说明
如果希望父类(.d.ts中的Prompto)能提供属性的默认实现,不能仅用declare class——declare关键字仅用于告诉TypeScript类的结构,不会生成实际的JavaScript代码。若需要父类具备实际属性逻辑,应将.d.ts改为普通.ts文件,编写真实的类实现而非声明式结构。
内容的提问来源于stack exchange,提问作者stop-ts
相关产品推荐
相关产品推荐

