Showing posts with label Typescript. Show all posts
Showing posts with label Typescript. Show all posts

23 Typescript interview questions and answers for beginners

Welcome to Typescript interview questions (pdf) and answers for beginners, Here are some tricky typescript interview question and answers collection which is asked in the interview. May it will help you to crack your typescript interview.

Typescript interview questions and answers for beginners
Typescript interview questions and answers

1. What is namespace in Typescript? Write the declaration of the namespace.

I think Everyone familiar with the namespace of other languages, here also in TypeScript a namespace is just a collection of classes by which we can logically categories. If we want to access any class from outside of the namespace then we need to export keyword.

namespace People
      export class Boss
      export class Employee
      export interface Iofficework


2. What is Typescript?

TypeScript is known as Object-oriented programming language developed and maintained by Microsoft. Which is provide you to optional parameters, classes, and interfaces.TypeScript is nothing but JavaScript and some additional features i.e. ES6 features. Whenever we compile the TypeScript code it first converts in JavaScript then it runs and we get output.

3.Difference between typescript and javascript?

1. Typescript is a superset of JavaScript whereas JavaScript is a scripting language.
2. Typescript has an interface concept but JavaScript havan't.
3.TypeScript doesn't run in the browser where JavaScript runs.
4.TypeScript has classes whereas JavaScript has functions.
5. Typescript support module whereas JavaScript doesn't support modules.
6. In typescript Number, a string is the interfaces whereas In, JavaScript Number, string are the Object.

4. What are the key features of typescript?

TypeScript supports different browsers or node.js versions and optimizes the resulting JavaScript. It is much easier to write clean, consistent code across a project and development team.

5.When to use a super keyword in typescript.

In typescript, Super keyword is used to reference base class properties and the base class constructor.

6.How to access static methods in TypeScript

7. What Are Classes In Typescript?

The concept of classes is very similar to .NET/Java. A Class can have constructor, member variables, properties, and methods. TypeScript also allows access to modifiers “private” and “public” for member variables and functions.

8. Tell me the command for installing typescript.

npm install -g typescript

9. What Are The Benefits Of Typescript?

TypeScript has the following benefits:

2Impose coding guidelines
3.Offers type checking
4. It helps in code structuring
5.Compile time error checking
6. Use class-based object-oriented programming

10. What is the typescript extension and how you compile?

Typescript extension is '.ts'. It is just a javascript file, when we compile typescript file, just after compilation it convert into '.js' file.

Step to compile a typescript program
1. tsc 'your typeScript File Name'
For example, to compile “program.ts”:
tsc program.ts

After compilation, you will get

11. How to declare a variable in Typescript?

var name:string="sham"
var phone:string="123456"

12. How do you declare an undefined variable?

var name:string;

var phone:int;

13. How to declare and initialize an array in Typescript?

var names:string[];       -- Declaration

names=["a","b","c","d"]   -- Initialization

14. How to achieve inheritance in Typescript?

15. How to make a module in Typescript?

16. What is the role of constructor in Typescript?

A constructor is responsible for initializing the variable of the class.

17. How to initialize the static variables?

class office
   static phoneNo:number=101;
   console.log(office.phoneNo) ;

18. What is the difference between var and let in Typescript?

1.var declaration done at function scoped and let declaration done at block scoped.
2.var allow you to re-declare the same variable in the same scope. whereas let will not

19.How to define a global variable in a typescript module.

20.How to export a variable.

export var demo = [1,2,3];

21. What is the role of var keyword in typescript?

var is key it helps us to create an object of a class.

22. How to Convert string to number in TypeScript?

   //convert string to number
   this.empcode = Number(name);

23. Can we debug Typescript file?

Yes, With the help of .js source map file.

24. What are the classes in TypeScript?

Typescript class is very similar to other languages like (.Net, Java). As we know that class can have constructor, member, variable and methods.

25. What are the benefits of Typescript?

1. ES7 and ES6 Features support.
2. User-friendly for Java and .Net programmer.
3. Intellisense.
4. Object Oriented programming support.

TAG: Typescript interview questions and answers for beginners, Typescript interview question and answers, beginner typescript question and answer

Read More

Hello world program in typescript

Hello world program

Today I am going to show you how to write simple hello world program in typescript, it is very simple to write helloword program and run it. Also we will talk about how to write a class in typescript and constructor in typescript.

Open the vs code and type the following single line of code and save it in your system's folder and follow the given steps.

console.log("Hello Word");

Step to compile and run the typescript program.

1.Save it with name helloword.ts
2.Open NodeJs Command Prompt , go to the directory where your helloword.ts file is saved.
3.In my case file available in E:\typescriptprtcl\New folder
4.So I am trying to go to in E:\typescriptprtcl\New folder using command.
Hello world program in typescript

5.Now do the following steps

1. tsc helloword.ts  when you type this command and hit enter the typescript program will be compile and one more file created with the name helloword.js  .See in your folder,here tsc means typescript compiler.
2. node helloword.js whenever you hit this command your helloword.js result show in picture.


tsc is compiler which convert the typescript file into javascript file so that we can run javacript, we can’t run directly typescript first we need to convert it.

Hello world program in typescript

How to write class in typescript and create object of class

class office
   empcode:any  ="Helloword";
   let obj=new office();

In above example any is a variable which is able to store any datatype in typescript. Let key use to create an object of a class so that we can call variable of the class. console.log  is same as Console.Write function in c#.

Constructor In typescript:

Constructor is special type of function which name same as class name in other language.  But in typescript if we want to create constructor , we need to write constructor keyword. See Example

class office
   let obj=new office(101);

Read More

Constructor chaining and inheritance in TypeScript

Typescript constructor and inheritance

Before going to understand the constructor chaining we first, need to know what exactly the typescript constructor is.

Constructor chaining

In typescript constructor is defined with keyword "constructor". The constructor is a special type of function of typescript class and it will be automatically invoked when the first object of the class is created.

Use of typescript constructor

Is to initialize a variable of the class

Typescript constructor Channing: 

Typescript classes support single inheritance the same as other languages.But, here we use the extends keyword to inherit classes in typescript.

  Class employee extends manager
             |                             |
             |                             |
     derive class                base class

Constructor chaining is just a process in which if you have a constructor in your class(derive class), then you need to call base class constructor by your constructor. Here constructor calling is different we are using the super keyword [super(d)].

class manager
        return this.duration;
class employee extends manager
    empdura :number;
    constructor (du:number,d:number)
        return this.empdura;

var emp=new employee(101,69);

When to use super keyword in typescript
1.Whenever we need to call the constructor of the base class in case of constructor chaining.

2. In most cases, to access the base class functions, the “super” the keyword is specified inside those functions which have the same name as the base class’s functions have in a derived class.

3. In a derived class, the overridden/hidden functions of a base the class can be accessed/called through a “super” keyword reference because along with a base class constructor the base class functions can also be accessed/refer through “super” keyword.

4. If a constructor is defined only in the derived class, the TypeScript compiler will generate a compile-time error until we use a “super ()” keyword in it.


Read More