javascript - Require JS Object and Functions in Typescript Angular2 project -
i working typescript 2.1.5 , trying write newer sections of code it. have js library not ready update in typescript. problem i need consume of javascript code in simple typescript component.
i have been fetching web way javascript functions called , work in .ts file. unfortunatly informations either outdated(mostly), not clear or not explained @ all. allready tryed whole lot of tricks none works.
so question : easiest way , simplest way consume javascript code in ts file (es6) ? there must protocol follow done.
any tutorial, link or explanations appreciated , i'am sure others in same situation.
for better understanding here code snippets :
i start doing import of js file in component.ts
var zp = require('./zp.js');
in js file have 2 types of functions. respectively basic 1 , methods object:
**export** function logtest(){ console.log("responding"); }//simple function
(*i found while editing, appending keyword "export" simple function makes avaible call in ts file. wich doesn't work methods *).
dobject.stringtest = function(){ return "responding"; } //object method
back component.ts file call each one. simple function export keyword gets called, dobject method remains undefined. (i made dummy calls dobject show doesn't work if can seem obvious some).
testfunc(event){ console.log("event okay"); console.log(zp.logtest()); <--- works console.log(zp.stringtest()); <--- don't work console.log(zp.dobject.stringtest()); <--- don't work console.log(zp.dobject.stringtest); <--- don't work }
here console logs :
dobject.stringtest not function
( first 2 calls stringtest() )
undefined
( last call dobject.stringtest )
( found declaration files, ll give shot, if works ll edit full explained step step tutorial. in mean time appreciated. )
in case existing javascript files not detected in typescript because typings not available.
you can create type definition (index.d.ts) file , keep adding typings available tsc compiler.
below few thing need understand when coming typescript world. of might know, may not.
every typescript file transpiled java script specific version want in tsconfig.json
typescript understand typescript ( javascript type definition required) or type definition.
- there many typing available in 2 forms :
- typings install
- @types namespace package
- import , export makes available members , properties others
Comments
Post a Comment