Types in TypeScript

Types in TypeScript

 
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. The 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:
 
start-project-in-type-script.gif
 
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: 
 
types-solution-explorer-type-script.gif
 
Coding
 
type.ts 
  1. function Greeter(greeting: string) {    
  2.  this.greeting = greeting;    
  3. }    
  4. Greeter.prototype.greet = function() {    
  5.  return "Hello User, " + this.greeting;    
  6. }    
  7. var greeter = new Greeter("Welcome to C-sharpcorner");    
  8. var button = document.createElement('button')    
  9. button.innerText = "Click to Say Hello"    
  10. button.onclick = function() {    
  11.  alert(greeter.greet())    
  12. }    
  13. document.body.appendChild(button)    
typedemo.html
  1. < !DOCTYPEhtml >    
  2.  <    
  3.  htmllanghtmllanghtmllanghtmllang = "en"    
  4. xmlns = "http://www.w3.org/1999/xhtml" >    
  5.  <    
  6.  head >    
  7.  <    
  8.  metacharsetmetacharsetmetacharsetmetacharset = "utf-8" / >    
  9.  <    
  10.  title > TypeScript HTML App < /title>   <    
  11.  linkrellinkrellinkrellinkrel = "stylesheet"    
  12. href = "app.css"    
  13. type = "text/css" / >    
  14.  <    
  15.  scriptsrcscriptsrcscriptsrcscriptsrc = "app.js" > < /script>   <    
  16.  /head>   <    
  17.  body >    
  18.  <    
  19.  h3 > Types Example in TypeScript HTML App < /h3>   <    
  20.  script > function Greeter(greeting) {    
  21.   this.greeting = greeting;    
  22.  }    
  23. Greeter.prototype.greet = function() {    
  24.  return "Hello User, " + this.greeting;    
  25. };    
  26. var greeter = new Greeter("Welcome to C-sharpcorner");    
  27. var button = document.createElement('button');    
  28. button.innerText = "Click to Say Hello";    
  29. button.onclick = function() {    
  30.  alert(greeter.greet());    
  31. };    
  32. document.body.appendChild(button); <    
  33. /script>   <    
  34. dividdividdividdivid = "content" / >    
  35.  <    
  36.  /body>   <    
  37.  /html>     
app.js
  1. function Greeter(greeting) {    
  2.  this.greeting = greeting;    
  3. }    
  4. Greeter.prototype.greet = function() {    
  5.  return "Hello User, " + this.greeting;    
  6. };    
  7. var greeter = new Greeter("Welcome to C-sharpcorner");    
  8. var button = document.createElement('button');    
  9. button.innerText = "Click to Say Hello";    
  10. button.onclick = function() {    
  11.  alert(greeter.greet());    
  12. };    
  13. document.body.appendChild(button);    
Output
 
 types-annotations-type-script.gif


Similar Articles