Typescript 扩展库的类型
我想用两个函数扩展bytebuffer库(),但我正在努力处理类型(它总是说“[ts]属性'myNewMethod'在类型'bytebuffer'上不存在”) 我的bytebuffer-sc.ts(应该扩展bytebuffer库):Typescript 扩展库的类型,typescript,typescript-typings,Typescript,Typescript Typings,我想用两个函数扩展bytebuffer库(),但我正在努力处理类型(它总是说“[ts]属性'myNewMethod'在类型'bytebuffer'上不存在”) 我的bytebuffer-sc.ts(应该扩展bytebuffer库): import { prototype } from 'bytebuffer'; // In the next line it says: Property 'myNewMethod' does not exist on type 'ByteBuffer'. pro
import { prototype } from 'bytebuffer';
// In the next line it says: Property 'myNewMethod' does not exist on type 'ByteBuffer'.
prototype.myNewMethod = (offset: number) => {
}
My src/types/bytebuffer-sc.extend.d.ts:
import { prototype } from 'bytebuffer';
// In the next line it says: Property 'myNewMethod' does not exist on type 'ByteBuffer'.
prototype.myNewMethod = (offset: number) => {
}
我尝试将“myNewMethod”添加到类型中的内容(不幸的是,没有成功):
更新(第二次尝试):
我的问题:
import { prototype } from 'bytebuffer';
// In the next line it says: Property 'myNewMethod' does not exist on type 'ByteBuffer'.
prototype.myNewMethod = (offset: number) => {
}
如何通过另一种方法正确扩展给定“类库”的类型?您的想法是正确的,但实现了错误的 扩展类型的声明必须是:
declare module 'bytebuffer' {
export interface ByteBuffer {
myNewMethod: number => number;
}
}
完成此操作后,可以将实际方法指定给原型:
ByteBuffer.prototype.myNewMethod = (offset: number) => { };
TypeScript需要您在分配属性/方法之前告诉它该属性/方法已存在。你把声明和实际作业混为一谈。事实上,我接受你的答案有点太快了。你的方法行不通。它仍然说这种方法不存在。另外:如果我将导入保留在.d.ts文件中,它将不再识别任何现有的类型定义。您可以发布新的.d.ts文件吗?这种方法应该有效。。。我已经用它扩展了RxJS的
可观察的
。基本上,我正在尝试修复我的项目的类型,因为有人在defineteytyped repo上创建了错误的类型。它应该是“CalculateArint32”,而不是“CalculateArint32”。我迫不及待地等待DefineteTypted接受我的拉取请求。