I work on a project that uses AngularJS heavily. Recently we wondered if using a preprocesser like CoffeeScript or TypeScript for our JavaScript would be beneficial. If our team is going to switch languages, we would need to be able to convert existing code over without much pain and we would have to find enough value in switching that it would be worth the conversion.
I had read an article that stated that because TypeScript is a SuperSet of JavaScript, you could convert a plain JavaScript file to TypeScript by changing the extension to .ts and not much else would need to change. I wanted to test out this claim, so I took a file that I was familiar with, an Angular Controller, and tried to convert it to TypeScript to see how much effort it would take and then try to figure out where we would benefit from using TypeScript.
This is what the controller JavaScript file looked like to start out with:
After changing the file's extension to .ts, in Visual Studio, I was greeted with Errors. Despite the IDEs telling me that there were Errors, the TypeScript file was still able to be compiled into a JavaScript file by just saving the .ts file, and the application continued to work without any changes to the file. While it was true for this file that it compiled without changing anything, the Errors would be a problem for building the entire solution.
The first Error I had to deal with was the fact that TypeScript did not know what angular was. It needed to be defined for the compiler to not complain about an object being used without any reference. Online resources lead me to discover that the "correct" way to solve this problem is to reference a definition file at the top of your TypeScript file. This is treated like a C# import, telling the compiler about that object. However, because I am trying to first do as little as possible to convert this file, I searched for and was able to find another approach that allowed the compiler to recognize angular without the need to reference any external files.
This is one area where the fact that TypeScript allows typing to be optional really helps out. I was able to declare the angular object as type "any". TypeScript sees the "any" type and allows for anything to be called on it, or for it to have any properties. This is basically like telling TypeScript to treat the object as a normal JavaScript object, which provides a lot of flexibility, but doesn't give you the benefits that TypeScript offers. In this case, the flexibility is worth it and we end up with:
Next Error is that TypeScript did not like the Angular $inject on the controller. Because the controller is not defined it treats it as a function, and functions don't have a $inject property. So I needed to similarly define the controller as an "any" object so that the compiler would stop complaining. This got rid of all of the Errors and we are left with this:
Unfortunately, after making this change, the application no longer works and I got JavaScript errors thrown by the browser. It treated controller as undefined at the point where it tried to add it in the angular .controller initialization. Surprisingly to me at least, defining the controller as a variable that is a function after trying to use it fails. The solution is to move the declaration up so that it happens before the controller is referenced anywhere. Leaving us with:
So in the end I had to make some changes, although they were very minor, to be able to get Visual Studio to compile without build Errors. With this conversion we don't get any real benefits from switching to TypeScript. Next up is to start digging into the features of TypeScript and find out how much value we can get from utilizing those features.
I had read an article that stated that because TypeScript is a SuperSet of JavaScript, you could convert a plain JavaScript file to TypeScript by changing the extension to .ts and not much else would need to change. I wanted to test out this claim, so I took a file that I was familiar with, an Angular Controller, and tried to convert it to TypeScript to see how much effort it would take and then try to figure out where we would benefit from using TypeScript.
This is what the controller JavaScript file looked like to start out with:
(function () { 'use strict'; angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; function controller($scope){ ... } })();
After changing the file's extension to .ts, in Visual Studio, I was greeted with Errors. Despite the IDEs telling me that there were Errors, the TypeScript file was still able to be compiled into a JavaScript file by just saving the .ts file, and the application continued to work without any changes to the file. While it was true for this file that it compiled without changing anything, the Errors would be a problem for building the entire solution.
The first Error I had to deal with was the fact that TypeScript did not know what angular was. It needed to be defined for the compiler to not complain about an object being used without any reference. Online resources lead me to discover that the "correct" way to solve this problem is to reference a definition file at the top of your TypeScript file. This is treated like a C# import, telling the compiler about that object. However, because I am trying to first do as little as possible to convert this file, I searched for and was able to find another approach that allowed the compiler to recognize angular without the need to reference any external files.
This is one area where the fact that TypeScript allows typing to be optional really helps out. I was able to declare the angular object as type "any". TypeScript sees the "any" type and allows for anything to be called on it, or for it to have any properties. This is basically like telling TypeScript to treat the object as a normal JavaScript object, which provides a lot of flexibility, but doesn't give you the benefits that TypeScript offers. In this case, the flexibility is worth it and we end up with:
declare var angular: any; (function () { 'use strict'; angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; function controller($scope){ ... } })();
Next Error is that TypeScript did not like the Angular $inject on the controller. Because the controller is not defined it treats it as a function, and functions don't have a $inject property. So I needed to similarly define the controller as an "any" object so that the compiler would stop complaining. This got rid of all of the Errors and we are left with this:
declare var angular: any; (function () { 'use strict'; angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; var controller: any = function($scope){ ... } })();
Unfortunately, after making this change, the application no longer works and I got JavaScript errors thrown by the browser. It treated controller as undefined at the point where it tried to add it in the angular .controller initialization. Surprisingly to me at least, defining the controller as a variable that is a function after trying to use it fails. The solution is to move the declaration up so that it happens before the controller is referenced anywhere. Leaving us with:
declare var angular: any; (function () { 'use strict'; var controller: any = function($scope){ ... } angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; })();
So in the end I had to make some changes, although they were very minor, to be able to get Visual Studio to compile without build Errors. With this conversion we don't get any real benefits from switching to TypeScript. Next up is to start digging into the features of TypeScript and find out how much value we can get from utilizing those features.
Fabulous post on angularjs. Really helpful, keep up the good work and share more like this.
ReplyDeleteAngularjs course in Chennai | Angularjs Training in Chennai
I got many useful informations about this topic from your blog. Really very useful for learning the skills
ReplyDeleteAngularJS Training in Tambaram
AngularJS Training in Porur
AngularJS Training in Velachery
Angularjs Training in chennai
AngularJS Training in T Nagar
Web Designing Training in OMR
Web Designing Training in T Nagar
Web Designing Training in Tambaram
Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
ReplyDeleteEducation
Technology
I got a Lot of knowledge from you. I need more to develop my skills. Thanks for this.
ReplyDeletejavascript training in chennai
core java training in chennai
C C++ Training in Chennai
c c++ courses in chennai
best c c++ training in chennai
C Language Training in Chennai
Appium Training in Chennai
JMeter Training in Chennai
An awesome blog. The author has really outdone himself this time. Great reading it.
ReplyDeleteGerman Classes in Mulund
French Classes in Mulund
IELTS Classes in Mulund
IELTS Coaching in Mulund
English Speaking Classes in Mulund
English Speaking Classes in Mulund West
English Speaking Course in Mulund
Spoken English Classes in Chennai
IELTS Coaching in Chennai
More impressive blog!!! Thanks for shared with us.... waiting for you upcoming data.
ReplyDeletethanks for your information really good and very nice web design company in velachery
REALY NICE...
ReplyDeletePython Internship
Dotnet Internship
Java Internship
Web Design Internship
Php Internship
Android Internship
Big Data Internship
Cloud Internship
Hacking Internship
Robotics Internship
NICE...
ReplyDeleteOracle Internship
R Programming Internship
CCNA Internship
Networking Internship
Artificial Intelligence Internship
Machine Learning Internship
Blockchain Internship
Sql Server Internship
Iot Internship
Data Science Internship
GREAT...
ReplyDeleteSelenium Testing Internship
Linux Internship
C Internship
CPP Internship
Embedded System Internship
Matlab Internship
nice
ReplyDeleteGeteventlisteners javascript
Karl fischer titration interview questions
How to hack tp link router
T system aptitude questions
Resume for bca final year student
Test case for railway reservation system
T systems pune placement papers
Infrrd bangalore interview questions
Max number in javascript
Paypal integration in php step by step pdf
good post...
ReplyDeleteFREE Internship in Nagpur For Computer Engineering Students
Internship For MCA Students
Final Year Projects For Information Technology
Web Design Class
Mechanical Engineering Internship Certificate
Inplant Training For Mechanical Engineering Students
Inplant Training Certificate
Ethical Hacking Course in Chennai
Winter Internship For ECE Students
Internships For ECE Students in Bangalore
great post...
ReplyDeleteHow To Hack On Crosh
Request Letter For Air Ticket Booking To HR
Zeus Learning Aptitude Paper For Software Developer
Cimpress Interview Questions
VCB Rating
Appreciation Letter To Vendor
JS MAX Safe Integer
Why Do You Consider Yourself Suitable For The Position
How To Hack Android Phone From PC
About Bangalore Traffic Essay
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGOOD NICE
ReplyDeleteiot training in coimbatore
summer training for 3rd year electronics and communication engineering students
goa current affairs 2019
project for information technology students
online internship for bca students
winter training for mechanical engineering students
ccna training
industrial training report for electronics and communication pdf
matlab courses in chennai
bba internship project
GOOD
ReplyDeleteResume Format For Bca Freshers
British Airways Interview Questions And Answers Pdf
Asus Tf101 Android 8
Crome://Flags/
T Systems Aptitude Test
Python Resume Ror 2 Years Experience
Ajax Redirect To Another Page With Post Data
Paramatrix Technologies Aptitude Questions And Answers
Adder Subtractor Comparator Using Ic 741 Op-Amp Theory
How To Hack Wifi With Ubuntu
ReplyDeleteThese blog concepts are new and unique it is easy to understand compared to other blogs. Keep updating this blogs.
DOT NET Training in Bangalore
DOT NET Training in Chennai
Dot NET Training in Marathahalli
DOT NET Training Institute in Marathahalli
DOT NET Course in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
Spoken English Classes in Bangalore
Great article. Thanks for sharing Such a worthy information....
ReplyDeleteSelenium Training in Chennai
Selenium Training in Bangalore
Selenium Training in Coimbatore
Best Selenium Training in Bangalore
Selenium Course in Bangalore
Selenium Training Institute in Bangalore
selenium training in marathahalli
Software Testing Course in Chennai
Hacking Course in Bangalore
Your post is just outstanding! thanx for such a post,its really going great and great work good work
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Transformation of marketing strategy
ReplyDeletewww.bluebase.in
https://www.facebook.com/bluebasesoftware/
https://www.linkedin.com/…/bluebase-software-services-pvt-…/
https://twitter.com/BluebaseL/
#applications #EnterpriseSolutions #CloudApplication #HostingServices #MobileAppDevelopment #Testing #QA #UIdesign #DigitalMarketing #SocialMediaOptimisation #SMO #SocialMediaMarketing #SMM #SearchEngineOptimisation #SEO #SearchEngineMarketing #SEM #WebsiteDevelopment #WebsiteDesigning #WebsiteRevamping
Well written Blog, I really enjoy reading your blog. this info will be helpful for me. Thanks for sharing.
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
A Great read for me, Well written technically on recent technology
ReplyDeleteAre you looking for Ethical hacking related job with unexpected Pay, then visit below link
Ethical Hacking Course in Chennai
Ethical Hacking Online Course
Ethical Hacking Course
Hacking Course
Hacking Course in Chennai
Ethical Hacking Training in Chennai
hacking course online
learn ethical hacking online
hacking classes online
best ethical hacking course online
best hacking course online
ethical hacking online training
certified ethical hacker course online
I liked this blog.. I got some clear information from this blog.. Thanks for taking a time to share this blog...
ReplyDeleteEthical Hacking Course in Porur
Ethical Hacking Course in OMR
Ethical Hacking Course in Tambaram
Ethical Hacking Course in Anna Nagar
Ethical Hacking Course in T Nagar
Nice post! it was so good to read and useful to improve my knowledge as an updated one, keep blogging.
ReplyDeletebenefits of seo for business
how to improve pronunciation skills in english pdf
how to pronounce english word
advantages of web designing
list of automation testing tools
salesforce developer interview questions
Never knew I can clear my debts so quick after my encounter with hacker boss at https://flipwuboss.me contact them if you looking for quick cash, thank me later
ReplyDeleteExtraordinary Blog. Provides necessary information.
ReplyDeletebest selenium training center in chennai
best training institute for selenium in chennai
Great post. keep sharing such a worthy information.
ReplyDeleteAngularjs Training in Chennai
Learn Angularjs Online
Angularjs Training In Bangalore
Very Interesting post. Thank you for sharing with us.
ReplyDeleteramanichandran novels
muthulakshmi raghavan novels pdf
sashi murali novels
tamil novels
srikala novels free download
mallika manivannan novels pdf download
tamil novel writers
Most individuals utilize short, easy to remember passwords. While seemingly harmless, simple and insecure passwords are one of the most common ways you can leave yourself susceptible to a cyberattack. Malware Analyst Career Overview
ReplyDeletePerde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
Ankara evden eve nakliyat
TRAFİK SİGORTASI
dedektör
web sitesi kurma
aşk kitapları
I loved the information given above, I appreciate the writing. Enhance your learning experience with our top-notch online home tuition classes for Class 11.
ReplyDeleteFor more info Contact us: +91-9654271931, +971-505593798 or visit online tutoring sites for class 11