Static vs Dynamic Type Checking

Rumman Ansari   Software Engineer   2023-12-05 05:47:13   28 Share
Subject Syllabus DetailsSubject Details 10 MCQ
☰ Table of Contents

Table of Content:


Static vs Dynamic Type Checking

Type System is a set of rules that assign a property called type to various construct a computer program consists of, such as variables, expressions, functions or modules. Type checker is responsible for Type checking. It is a kind of program analyzer verifying the types that are being used in the code. The sole purpose is to recognize the bugs before the execution of the code.

There are two types of type checking:

  • Static Type Checking
  • Dynamic Type Checking

Static Type Checking is done at compile time. The type of variable should be declared before using them. Examples of programming languages using Static Type Checking are C, C++, JAVA, C#, Fortran, Pascal, Scala etc.


var n: number; 	// declaration - required in static type checking
	n=5;	// definition - can be done later or in declaration
var m=6;
Static vs Dynamic type Checking

Dynamic Type Checking is done at run time. We do not have to declare the type of variables.

The compiler automatically detects the type. Examples are JavaScript, Python, VBScript etc.


	/* JavaScript code */	

	n=5;	
m=6;
sum=n+m;	

TypeScript has a distinctive feature of supporting the static typing. It concludes that we can declare the type of variable/ parameter/ function return.

Static typing finds its use only within TypeScript. When converted to a .js file, it has no more role there.

The main motive is to use it to compile time checking that ensures the right values are passed to each variable, making sure that the program behavior is same as expected.

Type Annotation - Variables and Arrays

In TypeScript, we define a type by just appending the variable name with colon followed by the type name as shown in below examples:

Defining Variables

var num: number = 3;

TypeScript infers the type with the value with which we would have initialized the variable. If we do not initialize the variable nor define a type when declaring a variable, TypeScript assigns “any” type to the variable.

var num = 3; // variable num is of type "number"

var num; // variable num is of type "any"

Defining Arrays

    let list: number[] = [1, 2, 3];


Type Annotation - Functions and Objects

Defining Functions

        let myAdd = ( x: number , y: number): number => { return x+y; }; 

// first two "number" defines type for parameters 'x' and 'y'

// third "number" defines `return type` of function
  • We can define optional parameter by adding “?” so that anyone calling that function may or may not pass value for that variable. Optional parameters do not exist in JavaScript and hence those will not be handled.
	var addFunction = (n1: number, n2: number, n3?: number) : number => {
		//observe "?" in parameter n3
		//Optional parameter has to be the last parameter in the list 
	}
	var sum = addFunction(10, 20);	//output: 30
  • We can define any parameter with a default value, and while calling the function, we are not required to pass the value of the parameters.
	var addFunction = (n1: number, n2: number, n3: number = 30) : number => {
		//observe parameter n3 has default value
	}

    var sum = addFunction(10, 20);	// output: 60

Defining Objects

	interface Point2D {
	    x: number;
	    y: number;
	}
     var point2D: Point2D = { x: 0, y: 10 }