Introduction
TypeScript adds optional static types to JavaScript. Types are used to place static constraints on program entities such as functions, variables, and properties so that compilers and development tools can offer better verification and assistance during software development. TypeScript's static compile-time type system closely models the dynamic run-time type system of JavaScript, allowing programmers to accurately express the type relationships that are expected to exist when their programs run and have those assumptions pre-validated by the TypeScript compiler.
All types in TypeScript are subtypes of a single top type called the Any type. Any keyword references this type.
All other types are divided into two categories.
Primitive types
The primitive types are the Number, Boolean, String, Null, and Undefined types. The number, bool, and string keywords reference the Number, Boolean, and String primitive types, respectively. It is not possible to explicitly reference the Null and Undefined types. Only values of those types can be referenced using the null and undefined literals.
Object types
The object types are all class, module, interface, and literal types. Class, module, and interface types are introduced through class, module, and interface declarations and are referenced by the name given to them in their declarations. Literal types are written as object, array, function, or constructor type literals and are used to compose new types from other types.
The following example shows how Types can be used in TypeScript. The example prints "Hello User, Welcome to C-sharpcorner".
Let's use the following steps.
Step 1. Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown as.
Give the name of your application as "type" and then click ok.
Step 2. After this session, the project has been created; your new project should look like this.
Coding
type.ts
function Greeter(greeting: string) {
this.greeting = greeting;
}
Greeter.prototype.greet = function() {
return "Hello User, " + this.greeting;
}
var greeter = new Greeter("Welcome to C-sharpcorner");
var button = document.createElement('button');
button.innerText = "Click to Say Hello";
button.onclick = function() {
alert(greeter.greet());
}
document.body.appendChild(button);
typedemo.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
<link rel="stylesheet" href="app.css" type="text/css" />
<script src="app.js"></script>
</head>
<body>
<h3>Types Example in TypeScript HTML App</h3>
<script>
function Greeter(greeting) {
this.greeting = greeting;
}
Greeter.prototype.greet = function () {
return "Hello User, " + this.greeting;
};
var greeter = new Greeter("Welcome to C-sharpcorner");
var button = document.createElement('button');
button.innerText = "Click to Say Hello";
button.onclick = function () {
alert(greeter.greet());
};
document.body.appendChild(button);
</script>
<div id="content"></div>
</body>
</html>
app.js
function Greeter(greeting) {
this.greeting = greeting;
}
Greeter.prototype.greet = function() {
return "Hello User, " + this.greeting;
};
var greeter = new Greeter("Welcome to C-sharpcorner");
var button = document.createElement('button');
button.innerText = "Click to Say Hello";
button.onclick = function() {
alert(greeter.greet());
};
document.body.appendChild(button);
Output