A Peek into TypeScript Succinctly | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

A Peek into TypeScript Succinctly

TypeScript is a rising star, offering web developers the static typing missing from JavaScript, but still fully compatible with it. Whether you’re familiar with JavaScript already, or you want an easier gateway to it first, TypeScript is an excellent choice for languages to learn. As usual, Syncfusion can get you up and running with the Succinctly series.

In TypeScript Succinctly by Steve Fenton, you will learn about TypeScript’s optional static typing and classes for JavaScript development, how to create and load modules, and how to work with existing JavaScript libraries through ambient declarations. TypeScript is even significantly integrated with Visual Studio to provide the autocompletion and type checking you are most comfortable with.

Some of what’s covered in only 82 pages:

  • TypeScript Concepts: Code Organization, Glossary, Compilation
  • Visual Studio Extensions
  • Type Safety
  • Creating New Modules
  • Loading Modules
  • How to Work with Existing JavaScript
  • Unit Testing with TypeScript

A glimpse inside this ebook:

Chapter 1

Concepts in TypeScript

Code organization I will begin by drawing some parallels between .NET and TypeScript in respect to code organization and language features. In some cases, the conventions are nearly identical, but there are also some interesting differences in both the naming conventions and the meaning of the various components used to compose your application.

Figure 1 demonstrates the structure of a TypeScript program, which is organized into a number of modules, each containing interfaces, classes, and variables. Each module should group related classes together and should have minimal dependencies on other modules in your program. Although you can create multidirectional dependencies and circular dependencies in TypeScript, I recommend that you avoid doing so, as it will make your program harder to maintain.

This program structure allows you to put your object-oriented programming skills into practice and makes it easy to use common design patterns to organize your program.

I will use the TypeScript naming conventions throughout this book, but feel free to read method when I use the term function, for example, if that is what you are more used to.

TypeScript glossary

Program

A TypeScript program is a collection of source files. Most of these source files will contain your code implementation, but they can also contain declarations that add static types to external code. I will talk about writing you own code in Chapter 4, “Creating new modules,” and consuming existing code and third-party libraries in Chapter 6, “Working with existing JavaScript.”

You can think of your program as both the TypeScript .ts files and the compiled JavaScript .js files. Module TypeScript modules are similar to .NET namespaces, and can contain a number of classes and interfaces. The structure of your modules can vary from very flat structures to highly nested ones depending on the kind of program you are writing.

Interface

Interfaces in TypeScript work exactly like .NET interfaces, allowing the development tools and the compiler to emit errors if a class fails to implement an interface correctly. Because the interface exists solely for the benefit of the design-time tools and the compiler, no JavaScript code is generated from a TypeScript interface. A TypeScript interface can extend multiple interfaces.

Class

A TypeScript class is similar to a .NET class and can contain functions and variables with varying levels of visibility. In TypeScript you can use public and private visibility keywords on functions and variables.

A TypeScript class can extend one class and implement multiple interfaces.

Function

A TypeScript function is like a .NET method, accepting arguments and returning a typed value or void. This is where the real logic will live in your program.

Variable

In TypeScript all properties and fields are variables, but with optional typing. By default, these variables behave in a similar manner to those declared in .NET using the var keyword, as the compiler will attempt to infer the type and then treat the variable as statically typed. In cases where the type cannot be inferred, the compiler doesn’t show an error as it would in .NET, but instead treats the variable as a dynamic type, which in TypeScript is indicated with the any keyword. I will discuss this in more detail in Chapter 3, “Type safety.”

Enumeration

Enumerations in TypeScript are similar to .NET enumerations. The experimental implementation in version 0.8 of TypeScript has been amended slightly in version 0.9.

Scope

Because functions, classes, and modules are compiled into various flavors of JavaScript functions, everything nested within them is locally scoped, and will enjoy the benefits of JavaScript’s lexical scoping. This means a nested function has access to its own variables and also the variables in the outer function. If no variable exists in the local scope, the JavaScript runtime walks up the chain to find the variable in the outer function, and if no variable exists there, it continues to the next level. This continues right up until JavaScript checks the global scope.

Compilation

TypeScript is converted into JavaScript at compile time. JavaScript is an implementation of the ECMAScript standard, and it isn’t the only one–ActionScript is another well-known language based on the ECMAScript standard. There are three versions of ECMAScript that you will come across when dealing with TypeScript.

ECMAScript 3

ECMAScript 3 was published in 1999, and it is the version supported in most browsers. When TypeScript compiles into JavaScript, it uses the ECMAScript 3 standard by default. A very small number of features are unavailable in TypeScript when targeting ECMAScript 3, such as property getters and setters, but it offers the widest possible support for your program.

If you try to use a feature that isn’t available in the version of ECMAScript you are targeting, the compiler will warn you to either avoid the feature or change the target version. I have included instructions on how to target ECMAScript 5 in Chapter 2, “Visual Studio.”

ECMAScript 5

ECMAScript 5 was published in 2009 and is supported in all modern browsers. You can instruct the TypeScript compiler to target ECMAScript 5, which makes additional language features available, but your program may encounter problems in older browsers. The following browsers support all of the important features of ECMAScript 5; older versions of these browsers have only partial support:

  • Internet Explorer 9 and above.
  • Firefox 4 and above.
  • Opera 12 and above.
  • Safari 5.1 and above.
  • Chrome 7 and above.

ECMAScript 6

ECMAScript 6, also known as ECMAScript Harmony, is currently being drafted and has experimental support in some of the latest browsers. This version is interesting, because it will make modules and classes part of the JavaScript language. TypeScript gives you immediate access to these proposed new features in practically all browsers. At some point in the future you will be able to target the ECMAScript 6 standard from TypeScript, which will mean the compiled JavaScript will more closely match your TypeScript code.

There is currently no release date planned for ECMAScript 6, and it doesn’t support all of the concepts in the TypeScript language.

TypeScript life cycle

The TypeScript life cycle consists of three distinct stages. When you are writing TypeScript code, you are participating in the design time stage along with any development tools you are using. The compiler represents the second stage, converting TypeScript into JavaScript, and raising errors and warnings it discovers. The final stage involves the runtime executing the generated JavaScript.

To continue reading TypeScript Succinctly, download it from our Succinctly library.

If you read this ebook and want to see or play with some TypeScript controls, check out Syncfusion’s Essential JS 2 suite, with over 50 components written in TypeScript and compiled in JavaScript. You can download them with a 30-day trial or a Community License, if you qualify.

Have you read TypeScript Succinctly or worked with our Essential JS 2 controls? Are you a fan of TypeScript? Let us know what you think in the comments below or on Facebook or Twitter.

If you like this blog post, we think you’ll also like the following free e-books:

JavaScript Succinctly
TypeScript Succinctly
AngularJS Succinctly
Angular 2 Succinctly

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed