CRM 2015 – Typescript is ready to go, definition files available on NuGet

I have written about Typescript before and I like the idea of Typescript so I want to promote it (and the work of Dave Berry).  Below is my previous Typescript article

What is TypeScript and how does it work with CRM 2011/2013

It’s time for an update.

I watched CRM MVP Dave Berry’s excellent presentation which is hosted on the XRMVirtual user group.

XRMVirtual group have lots of great past presentations, which you can find if you go to All Events –> Past

Not only did XRMVirtual group put on the presentation they put a recording so you can watch it if you missed the presentation

David Berry Presents an Introduction to TypeScript

I also recommend the Dave Berry CRM MVP Q&A, it’s informative and funny

It’s a LiveRecording which means you can download it watch it offline later.

The XRMVirtual group is a free group to join, so you have nothing to lose

I found watching the video very useful and it helped my understanding of how typescript can be used and how it works.

Quick recap of what Typescript is?

I will explain how I think and understand what TypeScript is and why’s its useful, this might not be the same view as everyone else.

Wiki definition first

TypeScript is a free and open source programming language developed and maintained byMicrosoft. It is a strict superset of JavaScript, and adds optional static typing and class-basedobject-oriented programming to the language. Anders Hejlsberg, lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript.[1][2][3][4]

TypeScript is designed for development of large applications and transcompiles to JavaScript.[5]As TypeScript is a superset of JavaScript, any existing JavaScript programs are also valid TypeScript programs.

Hosk description

Typescript works with Javascript and converts the typescript file into a javascript file.

What this means is you write your Typescript file and when you press compile it creates a plain Javascript version of your typescript file.  The end result is you end up with Javascript code, which is great because you can deploy this into Dynamics CRM.

Interesting why use Typescript?

Javascript is a dynamic language and .NET is strongly typed language.  What this means is when we have variables/objects/interfaces in .NET we write methods we define what variables/objects/templates should be passed.

If whilst developing some code a developer tries to call a method with a different type e.g. they try to pass an int into a method expecting a string, the compiler throws an error.

Typescript adds in this compiler/developer time type checking to your code

Hmmm I think I understand but I’m not convinced

The best way to explain the benefits of TypeScript is to compare it with CRM plugins using Early Bound.

Early bound code is creates a wrapper class round your entities.  It includes all the fields in your entity.  The benefits of of early bound classes

  • No typos
  • no type errors
  • easy to read and understand code

Early bound code means a lot of the potential errors in the CRM code are found at compile time instead of run time.  The big advantage of this is most of the errors are found and fixed by the developer rather than being found by the end customer.

Microsoft have a good post – Use the early bound entity classes in code

The advantages to using early-bound entity classes is that all type references are checked at compile time. The compiled executable contains the code necessary to invoke the types’ properties, methods, and events. For more information, see Use the early-bound entity classes for create, update, and delete.

Typescript is a bit like Early bound plugin code it brings the errors and type checking to compile time.

Early bound code uses an entity file (a file generated with all the fields on each entity) and Typescript uses a definitions file.

Typescript creates pure JavaScript files so the end result is the same as if you wrote JavaScript.

I’m interesting where can I find more information

My blog post

What is TypeScript and how does it work with CRM 2011/2013

Dave Berry’s Presentation

David Berry Presents an Introduction to TypeScript

TypeScript Introduction

What is typescript

Quick video

The video has a great quote, it’s preparing developers for what the future of Javascript will look like.

There are quite a few detailed videos on youtube, search for typescript

TypeScript available on NuGet

Dave Berry has been doing some great work creating the definition files for CRM 2013, CRM 2015, CRM 2015 service pack 1.

Dave has put in a lot of effort to help promote Typescript and CRM and he has succeeded on getting the typescript definition file on DefinitelyTyped(a website which holds definition files for typescript)

This is a great achievement because Dave had to put in a lot of effort creating tests for definition and this has (I believe) been a one man band effort.

You might be wondering why getting the definition files in NuGet is important, it means CRM developers can easily download the latest definition files right into visual studio.  This makes using typescript easier and will hopefully get more people trying typescript.

Hosk experience with Typescript

I have had a play with Typescript using Visual studio 2012.  The one question I had was it didn’t fit in very well with CRM Developer toolkit, there wasn’t a good development flow.  I was hoping Dave’s presentation would show me how this was done but unfortunately for me Dave uses Visual Studio 2013 (where Typescript is bundled in with Visual Studio) and he doesn’t use the CRM Developer toolkit.

I could easily create the TypeScript project and create the Javascript but it had to be in a separate project to my CRM Developer toolkit project, which made it a bit clunky.  If anyone has a good solution to this problem I would be interested

I’m at the stage where I need to do to use TypeScript more before I use the Javascript in a CRM project.  I also need to change my mindset from writing quick non OO Javascript and moving to writing more structured Javascript.

I’m not entirely sure how this fits in with a lot of the validation Javascript which is written for CRM projects but I think this is because I haven’t used Typescript much yet.

There is a little downside to using Typescript, except learning to write Typescript because the end result will be a pure Javascript file.

The benefits are strongly typed Javascript, this should capture lots of errors in compile time and create more readable code which should be well designed like server side code.  The advantage of well designed code is it will be easier to understand, maintain, debug and extend.  Basically the benefits lists in this post – Why your CRM code and customizations should be simple

 

Give it a go and leave me some comments of your adventures and links to blog posts about CRM and Typescript.

May the CRM force be with you

 

What is TypeScript and how does it work with CRM 2011/2013

I prodded CRM MVP Dave Berry with a twitter stick, berating him to start blogging again, this is dangerous because he is a CRM Super Hero

You should read his CRM MVP Q&A, it’s very funny and interesting

Th reason I was prompting him because I was reminded of his great blog posts because I was reading a blog post from Guido Preite who wrote an excellent blog entry –  Entity.GetAttributeValue<T> and ActivityParty, This is a great blog post from Guido and I would recommend all CRM Developers to read it (and subscribe to his blog).

In the blog he links to a Dave Berry blog post explaining Entity.GetAttributeValue.

Entity.GetAttributeValue Explained

This prompted me to tweet Dave Berry and he said he was doing some work on TypeScript and CRM.

What is TypeScript

Lets start with Wiki Definition

http://en.wikipedia.org/wiki/TypeScript

TypeScript is a free and open source programming language developed and maintained byMicrosoft. It is a strict superset of JavaScript, and adds optional static typing and class-basedobject-oriented programming to the language. Anders Hejlsberg, lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript.[1][2][3][4]

TypeScript is designed for development of large applications and transcompiles to JavaScript.[5]As TypeScript is a superset of JavaScript, any existing JavaScript programs are also valid TypeScript programs.

 

Stackoverflow has a good discussion

http://stackoverflow.com/questions/12694530/what-is-typescript-and-why-would-i-use-it-in-place-of-javascript

Typescript is a superset of Javascript which primarily provides static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code.

It’s still early days, but to get an idea of what I mean, watch Microsoft’s introductory video on the language.

For a large Javascript project, adopting Typescript might result in more robust software, while still being deployable where a regular javascript application would run.

It is open source, but you only get the clever Intellisense as you type if you use Microsoft’s Visual Studio (also noted in blog post from Miguel de Icaza).

 

Typescript home

http://www.typescriptlang.org/

This site has a bunch of samples, handbook and tutorials and is probably a good place to start.

Good articles on TypeScript

Some great resources for learning type script

http://definitelytyped.org/directory/learn.html

 TypeScript: First Impressions

Enhance Your JavaScript Investment with TypeScript

What is TypeScript

TypeScript: JavaScript Development at Application Scale

7 months

 

Hosk Thoughts on TypeScript

I will add I haven’t used TypeScript yet, so I’m just basing my thoughts on what I have read, whilst writing this article.  If you do want to get started using TypeScript with CRM, Dave’s article TypeScript, CRM 2011, and You is probably the best place to start.

As a Java developer and then C# I have often found JavaScript and it’s dynamic types confusing and dangerous.  It does have advantages like Using Dynamic Javascript.  Strong typing

The biggest problem I have is Javascript makes it hard to create classes and structure the code in logical and well thought out design.  The flip side of this it’s easy and tempting to right quick messy Javascript code which is a pain to debug and maintain later.  Javascript doesn’t offer much to the Developer with intellisense or code checking.  This means I can often publish code with syntax errors in it!!!

The first interesting point about TypeScript is it compiles the code to JavaScript, so the end product of your CRM development will be the same e.g. a javascript file.  You can almost think of it as the difference between writing early bound and late bound code.  The advantages of writing early bound plugin code, is the plugin code uses the entity classes and fields.  Early bound code has these advantages

  • Easy to read and understand
  • removes syntax errors
  • Casting and type errors are found at compile time (and not runtime by the customer)

TypeScript in my view is trying to do the same kind of standards and advantages to the JavaScript code as early bound code does to plugins.

One of the main advantages (in my opinion) is TypeScript makes it easy to create types, modules and interfaces in your code, which means the code will be more like the strong typing (C#) code.  This should make it easier for developers to create well structured code and less excuses for Developer to create hacky, messy Javascript.

Auto Complete/Intellisense improvements

Reading the http://www.typescriptlang.org/ page, another benefit is with strong typing (read this wiki article about strong and weak typing) is you can get visual studio tools/development tools to help with best practices, static checking and code re-factoring of the TypeScript code.  I understand this to mean it’s like writing plugins using early bound code, with Visual studio helping you easily select fields (defined in your entities file) and refactoring and check your code for best practices with tools like ReSharper and FXCop

The benefit of visual studio helping autocomplete all the Out of the box Javascript functions is probably worth it alone

Xrm.Page

Xrm.Data

etc

TypeScript was created by Microsoft

Microsoft created TypeScript to help .NET developers create similar code in JavaScript as they create in .NET.

 

CRM and TypeScript

There isn’t currently lots of resources or examples of TypeScript and CRM, I’m not suprised by this because until Dave mentioned I hadn’t really heard much about it.

Before you start creating any TypeScript I think I would start with the article written by Dave Berry

TypeScript, CRM 2011, and You

The blog post was from May 2013, so it will be a bit out of date (I’m guessing) but Dave has put these advantages of learning TypeScript

  • Types!  (If that wasn’t the first thing on this list, I should be soundly questioned.)
  • Compiles to JavaScript by basically removing itself from the code, and applying some cohesive closure structures to the results.  (This is why all valid JavaScript is valid TypeScript.)
  • C#’s interface and inheritance model!  (Multiple inheritance isn’t supported, but multiple interfaces are.  My C++ days are calling, and I let it go to voicemail.  I’m ok with this model.)
  • Multiple method signatures!  (This doesn’t apply anything on the “compile” to JavaScript—yet—but makes the IDE experience nicer.)
  • Generics!  (…coming in TypeScript 0.9)

 

CRM 2013

There is a good CRM 2013 blog post (split in 2) on TypeScript, which is simple and straight forward and a great place to start learning to use TypeScript

TypeScript and Dynamics CRM 2013 – Part 1

TypeScript and Dynamics CRM 2013 – Part 2

The CRM2013 definition file is on GitHub (https://github.com/sduck/Crm2013TypeScript)

 

CRM 2011

There are three blog posts I found on CRM 2011 and TypeScript

Using TypeScript for CRM 2011

TypeScript

TypeScript, CRM 2011, and You

To use TypeScript you need a definition file and for CRM 2011 you can find it

CRM 2011 TypeScript extension

 

 

Hopefully I will get a bit time to have a play with TypeScript myself, I love the idea of it and think it could a great tool to write JavasScript which would be easier to understand/maintain and with the improvements with intellisense would make less sytnax, complete waste of time type mistakes, which could save a bunch of wasted time publishing Javascript files.

This was my first look at TypeScript and the next step is to get my hands dirty and dive in.

If anyone else has any experience using TypeScript please leave a link to your experiences/blog post in the comments