I am in love with TypeScript, Here 12 TypeScript tips i wanna share …

From early days i started programming i don’t like JavaScript i like object oriented programming languages and its concepts like C#. when i heard about TypeScript i feel that my dream is coming true now and begin spend more time and time learning Typescript and its features. Simply, TypeScript is a language where you can write object oriented then it will be converted to javascript. the official site for TypeScript you can find on the following link:

TypeScript Blog

TypeScript on GitHub

Friends of TypeScript

TypeScript Versions

if you have installed TypeScript before on your environment you can get the version of TypeScript by executing the following command.

Let’s jump to TypeScript tips i would like to share. i didn’t write anything all tips are already there in TypeScript Handbook but i just need to highlight it.

Tip # 1 — Template Strings

It can span multiple lines and have embedded expressions. These strings are surrounded by the backtick (`) character, and embedded expressions are of the form ${ expr }.

Tip # 2 — Create Array of …

Two way to create array of type

Tip # 3 — Tuple

you need to represent as value as a pair of string and a number so you can use an array where the type of a fixed number of elements is known but need not be the same

Tip # 4 — enums

A helpful addition to the standard set of datatypes from JavaScript is the enum. As in languages like C#, an enum is a way of giving more friendly names to sets of numeric values.

Tip # 5 — any

We may need to describe the type of variables that we do not know when we are writing an application

The any type is a powerful way to work with existing JavaScript, allowing you to gradually opt-in and opt-out of type-checking during compilation. You might expect Object to play a similar role, as it does in other languages. But variables of type Object only allow you to assign any value to them - you can’t call arbitrary methods on them, even ones that actually exist:

The any type is also handy if you know some part of the type, but perhaps not all of it. For example, you may have an array but the array has a mix of different types

Tip # 6 — void

Declaring variables of type void is not useful because you can only assign undefined or null to them:

Tip # 7— never data type

The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns; Variables also acquire the type never when narrowed by any type guards that can never be true.

Tip # 8— Type Assertions

Type assertions are a way to tell the compiler “trust me, I know what I’m doing.” A type assertion is like a type cast in other languages, but performs no special checking or restructuring of data. It has no runtime impact, and is used purely by the compiler. TypeScript assumes that you, the programmer, have performed any special checks that you need.

Tip # 9 — let vs const

let and const are two relatively new types of variable declarations in JavaScript. As we mentioned earlier, let is similar to var in some respects, but allows users to avoid some of the common “gotchas” that users run into in JavaScript. const is an augmentation of let in that it prevents re-assignment to a variable.

Tip # 10 — Readonly Array

TypeScript comes with a ReadonlyArray<T> type that is the same as Array<T> with all mutating methods removed, so you can make sure you don’t change your arrays after creation

Tip # 11 — readonly vs const

The easiest way to remember whether to use readonly or const is to ask whether you’re using it on a variable or a property. Variables use const whereas properties use readonly.

Tip #12 — Index Signature (Dictionary Pattern)

Indexable types have an index signature that describes the types we can use to index into the object, along with the corresponding return types when indexing.

Above, we have a StringArray interface that has an index signature. This index signature states that when a StringArray is indexed with a number, it will return a string.

There are two types of supported index signatures: string and number. It is possible to support both types of indexers, but the type returned from a numeric indexer must be a subtype of the type returned from the string indexer. This is because when indexing with a number, JavaScript will actually convert that to a string before indexing into an object. That means that indexing with 100 (a number) is the same thing as indexing with "100" (a string), so the two need to be consistent.

we’re saying a SquareConfig can have any number of properties, and as long as they aren’t color or width, their types don’t matter.

In next posts, i will share more about real tips & issues i have faced during angular 2 + typescript implementation. :)