Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Classes in TypeScript

TypeScript classes 

TypeScript extends JavaScript classes to include type parameters, implements clauses, accessibility modifiers, member variable declarations, and parameter property declarations in constructors.

In following example created class employee with filed “fullname” and constructor which Constructs full name and assigned to class property.

TypeScript class code example:

class Employee {
    fullname: string;
    constructor(public firstName: string, public middleInitial: string, public lastName: string) {
        this.fullname = firstName + " " + middleInitial + " " + lastName;
    }
}
 
var user = new Employee("Shourya", "Nagnath", "Kendre");
 
window.onload = () => {
    var elFullNameName = document.getElementById('lblFullNameName');
    elFullNameName.innerHTML = user.fullname;
};

HTML code:

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
      <script src="ClassesExample.js"></script>
</head>
<body>
    <h3>Classes example</h3>
<label id="lblFullNameName"></label>
</body>
</html>
 

Result page: