python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Class field constructor function TypeScript

How to create fields, constructor and function in TypeScript Class?

What is class in TypeScript?

A class is a template definition of the methods and variables in a particular kind of object. It is extensible program, code and template for creating objects.

A TypeScript is object oriented JavaScript and it also supports object oriented programming features like classes, interfaces, etc.

A class captains the Public, Private, Protected and Read-only modifiers and Public by default.
You can see the below example, the class User and each members are public by default.

A class definition can contains the following –
1.     Fields
2.     Constructors
3.     Functions

Example – Use of class field, constructor and function i.e.
//Example 1- A simple class based example.
class User { // Calss
    name: string; //field
    constructor(nameTxt: string) { //constructor = nameTxt;

    getName() {//function
        return "Hello, " +;

let user = new User("Anil");//Creating Instance objects

How Static class in typescript?
We can define a class with static properties i.e.

export class Constants {
    static baseUrl = 'http://localhost:8080/';
    static date = new Date();

Ways to declare a nest class structure in typescript?

//Example 1-
declare module a{

    class b {

    module b {
        class c {

var clB = new a.b();
var clC = new a.b.c();

//Example 2-
export module a {
    export class b {

    export module b {
        export enum c {
            C1 = 1,
            C2 = 2,
            C3 = 3,

//Example 3-
class A {
    static B = class { }

var a = new A();
var b = new A.B();

Stayed Informed – Learn Angular 2 with TypeScript

I hope you are enjoying with this post! Please share with you friends. Thank you!!


Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions