例子:
class Temp {
static decode(buf: Uint8Array): Temp {
return new Temp();
}
}
function decode<T>(buf: Uint8Array): T {
return T.decode(buf);
}
let a = decode<Temp>(new Uint8Array());
不,这在打字稿中也是不可能的。
但是你可以做这样的事情:
class Base {
static decode<T>(buf: Uint8Array): T {
return instantiate<T>(); // `instantiate` is AssemblyScript's builtin
}
}
class Temp extends Base {
}
let a = Base.decode<Temp>(new Uint8Array(1));
如果您知道输出是引用,则可以像这样使用changetype
宏。
changetype<T>(changetype<usize>(T.decode(buf)));
您将负责确保解码函数的输出与T
数据类型的定义相匹配。
这并不安全,您需要知道自己在做什么。
谢谢
最有用的评论
不,这在打字稿中也是不可能的。
但是你可以做这样的事情: