【发布时间】:2021-01-23 22:43:00
【问题描述】:
我一直在解决这个问题一段时间,并认为我最终会理解或遇到一个体面的解释。我已经阅读了 TS 手册中的许多文章,google() 了它,并搜索了 SO,但还没有找到答案。
我需要知道如何在 TS 类/接口中正确且完整地键入 prototype 方法。
interface UploadFileInterface {
contentSizes: number[];
chunks: [];
chunksDone: number;
getChunkLength: (id: number) => void;
name: string;
size: string;
chunksQuantity: number;
}
class UploadedFile implements UploadFileInterface {
contentSizes: Array<number> = [];
chunks: [] = [];
chunksDone: number = 0;
getChunkLength: (id: number) => void;
^^^^^^^^ - 'Property 'getChunkLength' has no initializer and is not definitely assigned in the constructor.'
constructor(
public name: string,
public size: string,
public chunksQuantity: number
) {}
}
UploadedFile.prototype.getChunkLength = function(id: number): void {
}
我得到了这个可怕的“属性 'getChunkLength' 没有初始化器,并且没有在构造函数中明确分配。”错误。
我试图将 getChunkLength 完全从类和接口中移出,并保留它在原型上的编写方式(因为接口应该只描述类的“实例端”),但随后错误移动了到原型上的函数:
interface UploadFileInterface {
contentSizes: number[];
chunks: [];
chunksDone: number;
//getChunkLength: (id: number) => void;
name: string;
size: string;
chunksQuantity: number;
}
class UploadedFile implements UploadFileInterface {
contentSizes: Array<number> = [];
chunks: [] = [];
chunksDone: number = 0;
//getChunkLength: (id: number) => void;
constructor(
public name: string,
public size: string,
public chunksQuantity: number
) {}
}
UploadedFile.prototype.getChunkLength = function(id: number): void {
}
我觉得很奇怪,我似乎找不到更多关于这个场景的线索,就像手册中很明显或直接解释的东西一样,但我似乎找不到。你不应该直接在 TS 的原型中添加东西吗?我什至尝试在构造函数中使用 Object.setPrototypeOf() 来查看它是否会接受它,但没有运气。
我做错了什么?我应该只在 TS 中使用类并将方法直接放在类上吗?如果是这样,工厂函数如何适应 TS?有人可以告诉我解决这些情况的不同技巧吗?
【问题讨论】:
标签: typescript class methods prototype factory