我想通过两个函数扩展bytebuffer库(https://github.com/dcodeIO/bytebuffer.js),但我正在努力解决这些类型(它总是说" [ts]属性' myNewMethod&# 39;不存在类型' ByteBuffer'")。
我的bytebuffer-sc.ts(应该扩展bytebuffer lib):
import { prototype } from 'bytebuffer';
// In the next line it says: Property 'myNewMethod' does not exist on type 'ByteBuffer'.
prototype.myNewMethod = (offset: number) => {
}
我的src / types / bytebuffer-sc.extend.d.ts:
我试图添加" myNewMethod"对于类型(不幸的是没有工作):
import * as ByteBuffer from 'bytebuffer';
declare module 'bytebuffer' {
ByteBuffer.prototype.myNewMethod: (offset: number) => number;
}
更新(第二次尝试):
import * as ByteBuffer from 'bytebuffer';
declare module 'bytebuffer' {
export interface ByteBuffer {
calculateVarint32: (value: number) => number;
}
}
我的问题:
如何正确扩展给定"类库的类型"通过另一种方法?
答案 0 :(得分:2)
您已经有了正确的想法,但实施错误。
扩展类型的声明必须是:
$target_dir = "uploads/";
$base_name = basename($_FILES["file_to_upload"]["name"]);
$target_file = $target_dir.$base_name;//specifies the path of the file to be
uploaded.
$image_extention = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//rename the file
$rename_file_name = rename($base_name,'Image_1');
dd($rename_file_name);
执行此操作后,您可以将实际方法分配给原型:
declare module 'bytebuffer' {
export interface ByteBuffer {
myNewMethod: number => number;
}
}
TypeScript需要您在分配属性/方法之前告诉它存在属性/方法。您正在混合声明和实际作业。