TypeScript for busy C# Developers — 2: Primitive data types

In this post I will summarize the primitive data types used in TypeScript…

Boolean

  • C#: var flag: boolean = true;
  • TypeScript: var flag: boolean = true;

Number

  • C#: float num = 10.5;
  • TypeScript: var num: number = 10.5;
  • Notes: All numeric values in TypeScript (int, float, double, and decimal) are represented with a single type: number.

String

  • C#: string name = “Sameh Deabes”;
  • TypeScript: var name: string = “Sameh Deabes”;
  • Notes: single and double quotes will work in TypeScript strings.

Array

  • C#: int[] arr = { 1, 2, 3 }; arr[0] = 4;
  • TypeScript: var arr[]:number = [1, 2, 3]; arr[0] = 4;

Generis

  • C#: List<string> names= new List<string> { “Sameh”, “Serag”, “Deabes” };
  • TypeScript: var names:Array<string> = [“Sameh”, “Serag”, “Deabes”];

Null and Undefined

  • C#: object uninitialized= null;
  • null can be a value of reference type variables and nullable value type variables.
  • TypeScript: var uninitialized = null; var uninitialized2 = undefined;
  • undefined can be a value of all datatypes
  • null can be a value of all data types except except void and undefined.

Dynamic Types (no static type checking)

  • C#: dynamic unknown = 10; //in C# object datatype can be used for the same reason in TypeScript
  • TypeScript: var unknown: any = 10;

Void

  • C#: private void MethodWithNoReturnValue() { }
  • TypeScript: function methodWithNoReturnValue(): void { }

Type Inference

  • C#
  • var integer = 10; //int
  • var floatingPoint= 10.5; //double
  • var name = “Sameh”; //string
  • var x; //compilation error: Implicitly-typed variables must be initialized
  • TypeScript
  • var integer = 10; //number
  • var floatingPoint= 10.5; //number
  • var name = “Sameh”; //string
  • var x; //any

This post published first on my blog