I am having headaches trying to bring in an auto-generated java-script library into typescript...
I packed the java-script library and d.ts file into a npm package; and installed the npm package, and the typings modules in the host typescript environment; imported the defined classes in ts file
I have got something very weird:
"Sender" is an imported class in the following statement:
let sender: Sender = <Sender>cont.content(new Sender());
the typescript compiler reported "TS2304: Cannot find the name "Sender" with the type definition and casting; whereas the "new Sender()" is fine.
I posted the code snippets below for a better understanding. I had excerpted them for clearance
the 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;
...
the .ts file (the d.ts file is the same and acquired from .ts by tsc command
...
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;
the typescript host file
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();
}
}
}
the reported errors
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'.
the "Post" is an enum, and worked as expected.
Sender
. You have a variableSender
of typeSen_S
. In regards to "auto-generated java-script" What does this mean? Is it that you wrote it in typescript and generated the javascript? If so, why do you have interfaces representing classes? – chrisbajorin