我很头疼试图将自动生成的java脚本库引入typescript ...
我将java脚本库和d.ts文件打包到npm包中;并在主机打字稿环境中安装了npm包和typings模块;在ts文件中导入已定义的类
我有一些非常奇怪的东西:
“发件人”是以下声明中的导入类:
let sender: Sender = <Sender>cont.content(new Sender());
打字稿编译器报告“TS2304:无法找到名称为”Sender“的类型定义和转换;而”new Sender()“则没问题。
我发布了下面的代码段以便更好地理解。我摘录了他们的清关
js lib
/**
* @constructor
*/
function Exce() { ...}
Exce.getRootAsExce = function(bb, obj) { ...};
Exce.prototype.text = function(optionalEncoding) {...};
...
/**
* @constructor
*/
function Promp() { ...}
Promp.getRootAsPromp = function(bb, obj) {...};
Promp.prototype.text = function(optionalEncoding) {...};
...
/**
* @constructor
*/
function Sender() {...}
Sender.getRootAsSender = function(bb, obj) {...};
Sender.prototype.count = function() {...};
...
/**
* @constructor
*/
function Bucket() {...}
Bucket.getRootAsBucket = function(bb, obj) {...};
Bucket.prototype.content = function(obj) {...};
...
this.Exce = Exce;
this.Promp = Promp;
this.Sender = Sender;
this.Bucket = Bucket;
...
.ts文件(d.ts文件相同,并通过tsc命令从.ts获取
...
export interface Buc_S {
new (): Buc_I;
(): Buc_I;
getRootAsBucket (bb:any, obj:any):any;
...
}
export interface Buc_I {
contentType ():Post;
content (obj:any):any;
}
export var Bucket: Buc_S;
export interface Sen_S {
new (): Sen_I;
(): Sen_I;
getRootAsSender (bb:any, obj:any):any;
}
export interface Sen_I {
count(): number;
...
}
export var Sender: Sen_S;
export interface Pro_S {
new (): Pro_I;
(): Pro_I;
getRootAsPromp (bb:any, obj:any):any;
}
export interface Pro_I {
text (optionalEncoding: any): any;
}
export var Promp: Pro_S;
export interface Exc_S {
new (): Exc_I;
(): Exc_I;
getRootAsExce (bb:any, obj:any):any;
}
export interface Exc_I {
text (optionalEncoding: any): any;
}
export var Exce: Exc_S;
打字稿主文件
import {TransSender} from "./transSender";
import {Bucket, Post,Sender, Promp,Exce} from "fbsj";
export class Decoder{
public static extractSender (bucket: string):boolean{
let abs: Bucket = new Bucket();
let buf = flatbuffers.ByteBuffer(libbase64.decode(bucket));
let cont = Bucket.getRootAsBucket(buf, Bucket);
TransSender._type = cont.contentType();
if(TransSender._type == Post.Sender){
let sender: Sender = <Sender>cont.content(new Sender());
TransSender.setter(sender.count(), sender.pushed(), sender.read(), sender.saved(), sender.rated(), sender.followed(), sender.blocked(), sender.forwarded());
} else if(TransSender._type == Post.Promp){
TransSender._prompt = (<Promp>cont(new Promp())).text();
} else if (TransSender._type == Post.Exce){
TransSender._exception = (<Exce>cont(new Exce())).text();
}
}
}
报告的错误
Error:(24, 18) TS2304: Cannot find name 'Bucket'.
Error:(29, 25) TS2304: Cannot find name 'Sender'.
Error:(29, 35) TS2304: Cannot find name 'Sender'.
Error:(33, 37) TS2304: Cannot find name 'Promp'.
Error:(36, 40) TS2304: Cannot find name 'Exce'.
“帖子”是一个枚举,按预期工作。
答案 0 :(得分:0)
感谢cdbajorin的评论,
我改变了“Sender”的定义风格(类似于其他......),如下所示:
export interface Sender {
new (): Sender;
(): Sender;
getRootAsSender (bb:any, obj:any):any;
count(): number;
...
}
export declare let Sender: Sender;
似乎有效,
欢呼声