Typescript Flashcards
Explain Arrays in TypeScript
A collection of values of the same data type is called an array. It’s a kind that’s been defined by the user. To store values of the same kind, you use arrays. Arrays are collections of values that are ordered and indexed. The indexing begins at zero, with the first element having index 0, the second having index 1, and so on.
Syntax:
var array_name[:datatype]; //declaration
array_name = [val1,val2,valn..] //initialization
Example:
let values: number[] = [];
values[0] = 10;
values[1] = 20;
values[2] = 30;
List the Advantages of TypeScript
Problems are highlighted throughout development and at compilation time.
Typescript can be run in any browser or JavaScript engine.
A namespace notion is created by declaring a module.
IntelliSense is a TypeScript feature that provides active hints as you type.
Strongly typed or static typing is supported. The advantage of TypeScript is that it is strictly typed or allows for static typing. Because of static typing, it may confirm type correctness at compilation time.
List the disadvantages of TypeScript
It takes a long time to compile TypeScript code.
Abstract classes are not supported in TypeScript.
Converting TypeScript to JavaScript requires an additional compilation step.
Its type scheme is extremely complicated.
How to declare a variable in TypeScript?
Let and const are the two methods to declare variables.
Alphabets and numeric digits can both be used in variable names.
Except for the underscore (_) and the dollar ($) sign, they cannot contain spaces or special characters.
A digit can’t be the first character in a variable name.
let var a=10;
or
function f() {
var message = “Hello, world!”;
return message;
Name the access modifiers supported in TypeScrip
Protected- All child classes and members have access to them, but the instance does not.
Private- Only members have access
Public- Members, child classes, and instances of the class have access to the public modifier.
Explain Loops in Typescript
A loop statement allows to repeat the execution of a statement or a series of statements. To handle looping requirements, TypeScript provides a variety of loops.
For Loop
The for loop is a definite loop implementation. A definite loop is defined as a loop with a fixed number of iterations.
While Loop
When the condition supplied evaluates to true, the while loop executes the instructions.
Do..While Loop
The do…while loop is identical to the while loop, except that the condition isn’t evaluated the first time the loop runs.
You can convert a string to a number by using parseInt(), parseFloat() and Number(‘’) Method.
What is the Declare Keyword in TypeScript?
Since JavaScript lacks a TypeScript declaration, the declare keyword is used to include it in a TypeScript file without causing a compilation issue. Ambient methods and declarations use the term to define a variable that already exists.
How to create objects in Typescript?
Objects are collections of keys and values that resemble a dictionary. The keys must be one-of-a-kind. They resemble arrays and are sometimes referred to as associative arrays. An array, on the other hand, employs numbers to index the values, whereas an object lets you use any type as the key.
An Object type in TypeScript refers to any value with properties. It can be defined simply by specifying the properties and the kinds of those properties. As an example,
let pt: { x: number; y: number } = {
x: 10,
y: 20
};
Explain Different Data Types in Typescript?
Number
Number
Both Integer and Floating-Point numbers are represented by it.
Boolean
Boolean
True and false values are represented.
String
String
It’s used to denote a string of characters.
Void
Void
Usually applied to function return types.
Null
Null
It’s used when an object isn’t worth anything.
Undefined
Undefined
Indicates the value assigned to an uninitialized variable.
Any
Any
Any type of value can be assigned to a variable if it is declared with any data-type.
What is meant by Type Inference?
When you don’t specify an explicit type for a variable, TypeScript can infer it. Type inference is the term for this. This is normally done during the declaration, when the variables or parameters are initialized.
TypeScript recognises that the variable koo is a string, even if you don’t mention string as a type.
let koo = “Hello world”;
console.log(typeof koo); // “string”
Explain Tuples in Typescript With Example
Tuples are a collection of values that are diverse. It allows for the storage of many fields of various sorts. Tuples can also be used as function parameters.
There are instances when it is necessary to save a collection of values of various types. Arrays will not suffice in this situation. TypeScript provides a data type called tuple that aids in this endeavor.
Syntax:
var tuple_name = [value c,value b,value c,…value n]
For Example:
var yourtuple = [12,”Hi”];
What is Anonymous Function in TypeScript?
Anonymous functions are those that have no identifier (function name) attached to them. At runtime, these functions are dynamically declared. Anonymous functions, like normal functions, can accept inputs and return outputs. After its initial creation, an anonymous function is normally inaccessible. An anonymous function can be assigned to variables.
Syntax:
var res = function( [arguments] ) { … }
Example:
var msg = function() {
return “hello world”;
}
console.log(msg())
declarations
The Decorator is a type of declaration that is used to decorate a class declaration, method, accessor, property, or argument. Decorators take the form @expression, where expression must evaluate to a function that will be called with information about the decorated declaration when it is called at runtime.
Example:
function color(value: string) {
// this is the decorator factory, it sets up
// the returned decorator function
return function (target) {
// this is the decorator
// do something with ‘target’ and ‘value’…
};
}
classes
In terms of OOPs, a class is a template for producing objects. Object-oriented programming elements like as classes, interfaces, polymorphism, and data binding are all supported by TypeScript. The term “object” refers to a physical entity. Classes were not supported in JavaScript ES5 or earlier. This is a feature that Typescript inherits from ES6.
A class is a collection of items with similar characteristics. Fields, methods, constructors, Blocks, Nested classes, and interfaces are all included in the class.
Syntax to declare a class:
class class_Name{
field; method;
}