You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to write Custom Extension Types (Codecs) for TypeScript class but I got the following error.
Please let me know how I can fix this issue. Thanks!
my target TypeScript code
import * as msgpack from "msgpack-lite";
var codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);
var data = new MyVector(1, 2);
var encoded = msgpack.encode(data, {codec: codec});
var decoded = msgpack.decode(encoded, {codec: codec});
console.log("encoded = " + JSON.stringify(encoded));
console.log("decoded = " + JSON.stringify(decoded));
class MyVector {
x: number;
y: number;
constructor (x: number, y: number) {
this.x = x;
this.y = y;
}
}
// return Buffer serialized
function myVectorPacker(vector: MyVector) {
var array = [vector.x, vector.y];
return msgpack.encode(array);
}
// return Object deserialized
function myVectorUnpacker(buffer: Buffer) {
var array = msgpack.decode(buffer);
return new MyVector(array[0], array[1]);
}
compiled JS code
"use strict";
var msgpack = require("msgpack-lite");
var codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);
var data = new MyVector(1, 2);
var encoded = msgpack.encode(data, { codec: codec });
var decoded = msgpack.decode(encoded, { codec: codec });
console.log("encoded = " + JSON.stringify(encoded));
console.log("decoded = " + JSON.stringify(decoded));
var MyVector = (function () {
function MyVector(x, y) {
this.x = x;
this.y = y;
}
return MyVector;
}());
// return Buffer serialized
function myVectorPacker(vector) {
var array = [vector.x, vector.y];
return msgpack.encode(array);
}
// return Object deserialized
function myVectorUnpacker(buffer) {
var array = msgpack.decode(buffer);
return new MyVector(array[0], array[1]);
}
run time error
D:\work\msgpack-lite-cutome-extention-types>node .
D:\work\msgpack-lite-cutome-extention-types\node_modules\msgpack-lite\lib\write-core.js:40
var name = Class.name;
^
TypeError: Cannot read property 'name' of undefined
at Codec.addExtPacker (D:\work\msgpack-lite-cutome-extention-types\node_modules\msgpack-lite\lib\write-core.js:40:19)
at Object.<anonymous> (D:\work\msgpack-lite-cutome-extention-types\dist\index.js:4:7)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
The text was updated successfully, but these errors were encountered:
I want to write Custom Extension Types (Codecs) for TypeScript class but I got the following error.
Please let me know how I can fix this issue. Thanks!
my target TypeScript code
compiled JS code
run time error
The text was updated successfully, but these errors were encountered: