Typescript extract knowledge from js object - javascript

I'm creating a ionic (cordova) product and I'd like to utilize typescript and its benefits for autocompletion optimally. But since many of the plugin objects don't stem from concrete classes I don't know how/if there is a way to utilize the knowledge from the plugin files (which are .js). If there is a way to do this I'd of course like to know this.

As mentioned in my comment you can use Typescript definition files to provide type information.
There are definition files for common Cordova plugins on Definitly Typed.
If you can't find type information for the plugin you're looking for, you can write your own definition file.

If there is a way to do this I'd of course like to know this.
Yes. Just add allowJS to true in the tsconfig.json compilerOptions. Your IDE should magically start working with .js files ;)
More
Give http://alm.tools/ a go. I wrote it with such workflows in mind 🌹

Related

Guide for extending the Javascript language on VsCode for a 3rd party API

So I've started using VsCode over Atom recently and love it, the issue is I develop for software that uses its own JS API with no proper integration into anything.
I've started trying to implement my own autocomplete/intellisense structure using their pdf into VsCode to speed up my workflow, currently I'm simply using a JS file that is full of empty functions and objects with a bunch of JSDoc comments to help VsCode Intellisense identify what's what, that seems to be working fine so far but it means importing this "useless" file into every project I work on.
I looked into extending the JS language using a language server but that seems way too complex for what I need (plus it looks like I'd be building the entire Javascript language from scratch).
Does anyone have any recommendations ect?
Kind regards.
Edit
Sorry I also would like the ability for it to pass linting as right now it gets a bit funky with it.
You likely do not need an extension for that your example use case.
VS Code's intellisense for libraries is powered by .d.ts typing declaration files. The declaration files for the library you are using can either be written in your current workspace as you are currently doing, or—preferably—shipped with the library itself. Many npm modules ship their with typing definitions files, while other libraries have typing definition files provided by the community through DefinitelyTyped.
You alternatively bundle d.ts types file into as a separate npm package that you include in any project that needs them

Haxe externs for JS libraries

Does there exist any central place where all Haxe bindings for JS libraries are meant to be stored? Both if I want to find one, or submit one.
Is there some not outdated working tool to generate these bindings from TypeScript definitions?
Is there a binding for nodejs ws library?
http://lib.haxe.org also known as haxelib, the Haxe package manager. Other than that there are also libraries on Github, which you can install with haxelib git libname https://github.com/bla/bla.git
This kinda sounds like all libs are out of date. There is https://github.com/Simn/ts2hx as you might understand it is not a simple task to create such tool since TypeScript is a different (more dynamic) language and has a different type system which doesnt easily translate to haxe externs. In my experience this lib does 90% of the work. Also it isnt hard to create own externs, but I get you expect free direct-use libs. If tool/lib doesnt work please repot or contribute at the lib; Thats how opensource projects work.
I dont know
Hope this helps! Have a nice day!
Besides Haxelib, you can usually find a ton of stuff on GitHub (e.g. by searching for jsrequire <foo> language:haxe or other keywords). There are also some privately curated extern collections like clemos/js-kit, haxe-node-modules, abedev/npm
Simn/ts2hx is the only one that comes to mind
I found some externs, some of them inside other projects/libraries

TypeScript d.ts and .js consistency

Using these definitions:
https://github.com/borisyankov/DefinitelyTyped
So, say, I use angularJS 1.3.14, how to know for sure that there is proper definition for that particular angular version?
Or how to know for sure that DefinitelyTyped *.d.ts file is consistent to its *.js one?
Or If I use older version of AangularJS, how to find proper ts file?
How do I know it is a proper definition?
You use it and see if you come across a bug. Definitely Typed is open source and accepts issues and contributions so it is easy to remedy any errors in the definitions.
In the case of Angular, the large community will typically result in fewer errors in the definitions as many people are already using them.
How do I know the .d.ts is consistent to its .js one?
This is largely the same as above. This is a community endeavour, so there are many people updating type definitions to keep them correct. If you find a problem it is usually simple to solve (the type definition itself usually looks a lot like the API documentation for a library).
If I use an older version of Angular, how do I find the .d.ts file.
You can see the history on GitHub, for example the angular.d.ts history.
You can usually see when the commits go in to update the library versions here.

How do you attach JSdoc in Eclipse so that I can have autocomplete for a personal library

I have a JavaScript library that I am working on currently. I have structured it into lots of files and I use the module approach to define each 'module'.
var ns = generateNamespace("me.mycompany.mypackage.MyFile");
(function (ns, undefined) {
// some module
}(ns));
The modules are dynamically named using a namespacing function meaning that autocomplete is almost impossible as things stand (unless Eclipse can run my code and figure out the namespaces, Visual Studio can!).
Therefore I intend to generate JSdoc for my project in the hope that if I include this into Eclipse (somehow) Eclipse can use this to give me content assist.
Firstly I do not know if this is possible... however I think that it is as I can see that it is maybe how this works? However I tried to follow this along and struggled to get something working, by this I mean it didn't work. The interesting info from the link:
"JSDT libraries are collections of JavaScript source files that have prototyped object/class definitions and JSDoc. The inference engine then models these libraries... making them available to every JavaScript file in the project... Bindings for nonstandard and future runtimes are similarly easy to create... add the... library to their project and gain content completion and hover help"
I know how to write JSdoc annotations and I know how to generate JSdoc using one of the various tools.
What I need therefore is instructions on how to include JSdoc (as a library maybe) in Eclipse so that it will give auto complete for the stuff in the JSdoc.
Previous answer:
You can also run jsdoc_toolkit from within eclipse by setting up Run -
> External Tools -> Open External Tools Dialog...
Location
C:\Program Files\Java\jre1.5.0_12\bin\java.exe
Working Directory
C:\DirectoryToWhereJsDocToolkitIsLocated\jsdoc_toolkit
Arguments
-jar app/js.jar app/run.js -r=4 -t=templates/htm "-d=C:
\PathToWhereDocIsSaved" "C:\PathToWebsiteToDocument"
For more about the arguments check the jsdoc_toolkit documentation.
Have fun
Simon
Taken from this Google Groups thread.
Updated Answer:
Eclipse JavaScript Editor: content assist for js files, autocompletion
Disclaimer, I'm the author of tern.java.
I suggest you that you install tern.java. It provides a JSDoc support. Once you have selected this support, you can benefit
with completion :
and soon with validation:
This support is not perfect but it starts working.
I am not using it by myself, so I'm not sure if it works, but there exists a grunt-plugin for jsdoc3. Grunt is supported by Eclipse. So maybe it helps.
Grunt-PlugIn in npm

#required annotation in javascript files

I am at the starting point of trying to build a JavaScript library for our RIA application at work. Currently we have lots of JavaScript files with functions all in global scope and I would like to attempt to put some reasonable namespace and context to our functions. Possibly putting them into object form.
I've been looking at other open source libraries to help me understand the best way to build a library. I see a lot of .js files with the following in one particular open-source project.
/**
* #required OpenLayers/Map.js
*/
for example.
Does this have any meaning other than just being a comment in the file?
Is there some kind of way to import other .js files into your main build with this annotation or an import statement like in Java?
This code is using JsDoc (http://jsdoc.sourceforge.net/) or something similar to create documentation from the source code, most likely.
Javascript doesn't actually have annotations.
Choose RequireJS. It is a JavaScript file and module loader. It implements the CommonJS proposal.
It's a PHPDoc-style comment, telling anybody working with the code that it will not work properly without OpenLayers/Map.js
JavaDoc, the grandaddy of all code documentation/annotation systems for which all others (I think) are derived:
http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html

Categories

Resources