TypeScript tutorial: Lesson 1


Install nodejs or nvm

For Windows: Node Version Manager (nvm) for Windows

IDE Visual Studio Code:
https://code.visualstudio.com/docs?dv=win

TypeScript Playground
https://www.typescriptlang.org/play?#

Install typescript

npm install -g typescript

Result:

C:\Users\Administrator>npm install -g typescript
C:\Program Files\nodejs\tsserver -> C:\Program Files\nodejs\node_modules\typescr
ipt\bin\tsserver
C:\Program Files\nodejs\tsc -> C:\Program Files\nodejs\node_modules\typescript\b
in\tsc
C:\Program Files\nodejs
`-- typescript@3.9.7

Verify:

C:\Users\Administrator>tsc --version
Version 3.9.7

Compile typescript

tsc file1.ts, file2.ts, file3.ts

Identifiers in TypeScript

  • Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
  • Identifiers cannot include special symbols except for underscore (_) or a dollar sign ($).
  • Identifiers cannot be keywords.
  • They must be unique.
  • Identifiers are case-sensitive.
  • Identifiers cannot contain spaces.

TypeScript ─ Keywords
Keywords have a special meaning in the context of a language. The following table lists some keywords in TypeScript.

break as any switch
case if throw else
var number string get
module type instanceof typeof
public private enum export
finally for while void
null super this new
in return true false
any extends static let
package implements interface function
new try yield const
continue do catch yeild
async await of self

Comments in TypeScript

  • Single-line comments ( // )
  • Multi-line comments (/* */)

TypeScript and Object Orientation

  • Object − An object is a real time representation of any entity. According to Grady Brooch, every object must have three features −
    • State − described by the attributes of an object
    • Behavior − describes how the object will act
    • Identity − a unique value that distinguishes an object from a set of similar such objects.
  • Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object.
  • Method − Methods facilitate communication between objects.

Example:

class Animal { 
   say():void { 
      console.log("Hello World!!!") 
   } 
} 
var obj = new Animal(); 
obj.say();

After compiling

var Animal = /** @class */ (function () {
    function Animal() {
    }
    Animal.prototype.say = function () {
        console.log("Hello World!!!");
    };
    return Animal;
}());
var obj = new Animal();
obj.say();

TypeScript – Types

The Any type

The any data type is the super type of all types in TypeScript. It denotes a dynamic type. Using the any type is equivalent to opting out of type checking for a variable.

Built-in types

Data type Keyword Description
Number number Double precision 64-bit floating point values. It can be used to represent both, integers and fractions.
String string Represents a sequence of Unicode characters
Boolean boolean Represents logical values, true and false
Void void Used on function return types to represent non-returning functions
Null null Represents an intentional absence of an object value.
Undefined undefined Denotes value given to all uninitialized variables
Symbol symbol
Unknown unknown We may need to describe the type of variables that we do not know when we are writing an application. These values may come from dynamic content

Example

let notSure: unknown = 4
notSure = "maybe a string instead"
// OK, definitely a boolean
notSure = false
Never never represents the type of values that never occur.

Example

function error(message: string): never {
  throw new Error(message)
}
Object object is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, symbol, null, or undefined.

Example

var str = typeof(Object.create({}))
console.log(str) //object

User-defined Types
User-defined types include Enumerations (enums), classes, interfaces, arrays, and tuple.

Leave a Reply