TypeScript is a programming language developed and maintained by Microsoft.
It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for the development of large applications and transcompiles to JavaScript.
TypeScript is a superset of JavaScript that offers excellent consistency.
It is highly recommended, as it provides some syntactic sugar and makes the code base more comfortable to understand and maintain. Ultimately, TypeScript code compiles down to JavaScript that can run efficiently in any environment.
TypeScript can be executed on Any browser, Any Host, and Any Operating System. TypeScript is not directly run on the browser. It needs a compiler to compile and generate in JavaScript file. TypeScript is the ES6 version of JavaScript with some additional features.
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.
The indexing begins at zero, with the first element having index 0, the second having index 1, and so on.
The value is coming from an API call or the user input. The "any" type allows you to assign a value of any type to the variable of type any.
TypeScript assumes a variable is of type any when you don’t explicitly provide the type.
The void indicates the absence of type on a variable. It acts as the opposite type to any. It is especially useful in functions that don’t return a value.
If a variable is of type void, you can only assign the null or undefined values to that variable.
Let and const are the two methods to declare variables.
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.
Alphabets and numeric digits can both be used in variable names.
Language:
The language comprises elements like new syntax, keywords, type annotations, and allows us to write TypeScript.
Language Service:
The language service provides information which helps editors and other tools to give better assistance features such as automated refactoring and IntelliSense.
Compiler:
The TypeScript compiler is open source, cross-platform, and is written in TypeScript. It transforms the code written in TypeScript equivalent to its JavaScript code. It performs the parsing, type checking of our TypeScript code to JavaScript code.
The unknown type is the type-safe counterpart of any type. You can assign anything to the unknown, but the unknown isn’t assignable to anything but itself and any.
The typescript was developed by Anders Hejlsberg, who is also one of the core members of the development team of C# language. The typescript was first released in the month of October 1st, 2012 and was labeled version 0.8.
It is developed and maintained by Microsoft under the Apache 2 license. It was designed for the development of a large application.
The current stable version of TypeScript is 4.6.2 which was released on 01 March 2022.
Functions are blocks of code to perform a specific code. Functions can optionally take one or more arguments, process them, and optionally return a value.
Objects are dictionary-like collections of keys and values. The keys have to be unique.
They are similar to arrays and are also sometimes called associative arrays.
However, an array uses numbers to index the values, whereas an object allows you to use any other type as the key.
To install TypeScript, first ensure that the npm is installed correctly, then run the following command which installs TypeScript globally on the system.
When a variable is declared without initialization, it’s assigned the undefined value. It’s not very useful on its own. A variable is undefined if it’s declared, but no value has been assigned to it.
The never type is used when you are sure that something is never going to occur.
For example, a function that never returns a value or that always throws an exception can mark its return type as never.
You might wonder why we need a ‘never’ type when we already have 'void'?
A function that doesn't return a value implicitly returns the value undefined in JavaScript. Hence, even though we are saying it’s not returning anything, it’s returning ‘undefined’. We usually ignore the return value in these cases.
Interface defines the syntax that any entity must attach to.
An Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, it means a class that implements an interface is bound to implement all its members.
It cannot be instantiated but can be referenced by the class object that implements it. The TypeScript compiler uses interface for type-checking whether the object has a specific structure or not.
The interface just declares the methods and fields. It cannot be used to build anything. Interfaces need not be converted to JavaScript for execution.
Class in term of OOPs is a blueprint for creating objects. It is a group of objects which have common properties.
TypeScript supports object-oriented programming features like classes, Interfaces, Polymorphism, data-binding etc.
Object means this a real world entity. JavaScript ES5 or earlier didn’t support classes. Typescript inherits this feature from ES6.
We know, TypeScript is a type of Object-Oriented JavaScript language and supports OOPs programming features like classes, interfaces, etc.
Like Java, classes are the fundamental entities which are used to create reusable components.
It is a logical entity. The "class" keyword is used to declare a class in Typescript.
Features of a class are:
TypeScript type checker uses the type of the window.onmousedown function to infer the type of the function expression.
It was able to infer the type of the mouseEvent parameter, which contains a button property, but not a foobar property.
When one side of the equation has type but the other does not, the TypeScript compiler determines the form.
You can skip typing on the right side because TypeScript will figure it out for you. This reduces the amount of time spent typing code.
No. modules are not supported by Native JavaScript.
A rest parameter allows a function to accept an indefinite number of arguments as an array. It is denoted by the '…' syntax and indicates that the function can accept one or more arguments.
When the number of parameters that a function will receive is not known or can vary, we can use rest parameters.
We can pass zero or more arguments to the rest parameter. The compiler will create an array of arguments with the rest parameter name provided by us.
In TypeScript, you can add a type annotation to each formal parameter of a function using a colon and the desired type.
That way, your code doesn't compile when you attempt to call the function with an argument of an incompatible type, such as number or boolean. Easy enough.
You can use the object de-structuring syntax:
ES6 version of TypeScript provides an arrow function which is the shorthand syntax for defining the anonymous function
Arrow functions provide a short and convenient syntax to declare functions. They are also called lambdas in other programming languages.
Arrow functions are often used to create anonymous callback functions in TypeScript.
We can split the syntax of an Arrow function into three parts:
Modules are a way to organize your code into smaller, more manageable pieces, allowing programs to import code from different parts of the application.
A module is a powerful way of creating a group of related variables, functions, classes, and interfaces.
It can be executed within its own scope, but not in the global scope. Basically, you cannot access the variables, functions, classes, and interfaces declared in a module outside the module directly.
A module can be created by using the export keyword and can be used in other modules by using the import keyword.
A Decorator is a special kind of declaration that can be applied to classes, methods, accessor, property, or parameter.
Decorators are simply functions that are prefixed @expression symbol, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.
To enable experimental support for decorators, we must enable the experimentalDecorators compiler option either on the command line or in our tsconfig.json:
Usually, when we don’t provide any type on a variable, TypeScript assumes ‘any’ type.
noImplicitAny is a compiler option that you set in the tsconfig.json file. It forces the TypeScript compiler to raise an error whenever it infers a variable is of any type. This prevents us from accidentally causing similar errors.
Define an abstract class in Typescript using the abstract keyword. We cannot create an instance of an abstract class.
Abstract classes are similar to interfaces in that they specify a contract for the objects, and you cannot instantiate them directly. However, unlike interfaces, an abstract class may provide implementation details for one or more of its members.
An abstract class marks one or more of its members as abstract. Any classes that extend an abstract class have to provide an implementation for the abstract members of the superclass.
Anonymous Function is a function that does not have any name associated with it.
An anonymous function is a function without a name. Anonymous functions are typically used as callback functions. Normally we use the function keyword before the function name to define a function, however, in anonymous functions, we use only the function keyword without the function name.
A union type is a special construct in TypeScript that indicates that a value can be one of several types. A vertical bar (|) separates these types.
Union types allow you to create new types out of existing types.
However, if we try to set the value to a type not included in the union types, we get the error.
Intersection types allow us to combine two or more types into one. The resulting type will have all the properties of all the types.
This allows us to get a Single type from existing types that has all the properties of both the types
Intersection types let you combine the members of two or more types by using the ‘&’ operator. This allows you to combine existing types to get a single type with all the features you need.
A mixin class is a class that implements a distinct aspect of functionality. Other classes can then include the mixin and access its methods and properties. That way, mixins provide a form of code reuse that is based on composing behavior.
In Javascript, Mixins are a way of building up classes from reusable components and then build them by combining simpler partial classes.
The idea is simple, instead of a class A extending class B to get its functionality, function B takes class A and returns a new class with this added functionality. Here, function B is a mixin.
Yes, it is possible. To debug any TypeScript file, we need .js source map file. So compile the .ts file with the --sourcemap flag to generate a source map file.
Type aliases give a new, meaningful name for a type. They don’t create new types but create new names that refer to that type.
We know that all JavaScript libraries/frameworks don't have TypeScript declaration files, but we want to use them in our TypeScript file without any compilation errors. To do this, we use the declare keyword. The declare keyword is used for ambient declarations and methods where we want to define a variable that may exist elsewhere.
TypeScript runtime will assign the myLibrary variable as any type.
Triple-slash directives are single-line comments that contain a single XML tag. TypeScript uses this XML tag as a compiler directive.
You can only place a triple-slash directive at the top of the containing file. Only single or multi-line comments can come before a triple-slash directive.
The primary use of triple-slash directives is to include other files in the compilation process.
The scope is a set of objects, variables, and function and the JavaScript can have a global scope variable and local scope variable.
Yes, TypeScript support all object-oriented principles.
In TypeScript, the 'as' syntax is used for Type assertion. It was created because the original syntax was incompatible with JSX. Only as-style assertions can be used with JSX and TypeScript.
TSD is a package manager to search and install TypeScript definition files directly from the community driven DefinitelyTyped repository.
Function parameters can be assigned values by default. A parameter can’t be declared as optional and default both at the same time.
Ambient declarations are a way of telling the TypeScript compiler that the actual source code exists elsewhere.
Typescript provides a way for using third-party libraries like Node.js, jQuery, AngularJS, etc. in an easier and safer manner. These are done with the help of Ambient declarations.
Ambient Declarations are a method which informs the TypeScript compiler that the real source code (like functions or variables) exists in another place. If we attempt to utilize these source codes when they do not exist at runtime, then without giving hint it will break. We have to always write ambient declarations if our TypeScript code needs to use third-party JavaScript libraries such as jQuery, AngularJS, Node.js, etc.
The Ambient declarations allow us to safely and easily use existing popular JavaScript libraries like jquery, angularjs, nodejs, etc.
When using these JavaScript libraries such as jQuery, AngularJS, Node.js, etc, then validating code completion and typesafety becomes difficult for a TypeScript programmer. So, Ambient declarations assist to directly integrate other JavaScript libraries into TypeScript.
Template literal types are similar to the string literal types. You can combine them with concrete, literal types to produce a new string literal type. Template literal types allow us to use the string literal types as building blocks to create new string literal types.
Until TypeScript 4.1, we had three literal types: strings, numbers, and booleans. TypeScript 4.1 introduced the fourth literal type: template literal.
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.
At times, there might be a need to store a collection of values of varied types. Arrays will not serve this purpose. TypeScript gives us a data type called tuple that helps to achieve such a purpose.
Function is a global type in TypeScript. It has properties like bind, call, and apply, along with the other properties present on all function values.
Copyright © 2024 Programming Path
This website uses cookie or similar technologies, to enhance your browsing experience and provide personalised recommendations. By continuing to use our website, you agree to our Cookie Policy.