Typescript Flashcards
What is typescript
TypeScript is a superset of JavaScript, meaning that JavaScript code is valid TypeScript code. TypeScript adds rules about how values can be used but doesn’t consider JavaScript code to be in error based on syntax alone.
TypeScript is a typed superset, which means it introduces static typing rules. It helps catch type-related errors during development without changing the runtime behavior of JavaScript code.
When you move code from JavaScript to TypeScript, it guarantees the same runtime behavior. TypeScript preserves JavaScript’s behavior, ensuring your program runs the same way. This allows for an easy transition between the two languages without worrying about subtle differences.
By preserving JavaScript’s runtime behavior and providing static typing, TypeScript enhances development without introducing incompatible changes, making it a powerful and seamless extension of JavaScript.
difference between typescript and javascript
TypeScript adds additional features and capabilities on top of JavaScript, such as static typing, interfaces, classes, modules, and more. By learning TypeScript, you will gain a deeper understanding of these advanced language features and how they can be utilized in your code.
TypeScript does introduce some additional syntax and concepts that are not present in JavaScript. This includes type annotations, TypeScript-specific decorators, and other language constructs.
When should you use TypeScript?
When you have a large codebase
When your team’s developers are already accustom to statically-typed languages
If you or the majority of the team come from a strongly typed language like C# or Java, and don’t want to go all-in on JavaScript, TypeScript is a good alternative.
Simple types
Boolean: Represents the logical values true or false.
Number: Represents numeric values, including integers, floating-point numbers, and special numeric values like NaN and Infinity.
String: Represents textual data enclosed in single quotes (‘) or double quotes (“).
Null: Represents the intentional absence of any object value. It is a unique type that has only one possible value: null.
Undefined: Represents a variable that has been declared but has not been assigned any value yet. It is a type that has only one possible value: undefined.
Symbol: Represents unique and immutable values that can be used as property keys in objects.
BigInt: Represents arbitrary precision integers, enabling you to work with numbers larger than the maximum representable number in JavaScript’s Number type.
object types
In TypeScript, object types refer to types that represent structured objects with properties and methods:
Object Literal Type: You can define an object type using an object literal syntax, specifying the property names and their corresponding types. For example:
typescript
Copy code
let person: { name: string; age: number } = { name: “John”, age: 25 };
Interface: Interfaces are a powerful way to define object types in TypeScript. They allow you to specify the names, types, and optional/required status of properties, as well as define methods. For example:
typescript
Copy code
interface Person {
name: string;
age: number;
greet: () => void;
}
let person: Person = {
name: “John”,
age: 25,
greet: () => {
console.log(“Hello!”);
},
};
Class: Classes in TypeScript can be used to define object types with properties and methods. You can define a class with properties and member functions, and then create instances of that class. For example:
typescript
Copy code
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet() {
console.log(“Hello!”);
}
}
let person: Person = new Person(“John”, 25);
TypeScript has special types that may not refer to any specific type of data.
any is a type that disables type checking and effectively allows all types to be used.
unknown is a similar, but safer alternative to any.
typescript doesn’t allow you to use a variable of unknown type unless you either cast the variable to a known type or narrow its type
Type narrowing is the process of moving a less precise type to a more precise type.
The unknown type forces you to determine what a variable typed as unknown is, either through type casting or type narrowing.
This in turn leads to better programming, as typescript can then type check the resulting type, leading to a more type-safe program.
Alias
A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program.
Type aliases do not create new types. They simply provide a new name to an existing type.
The main purpose of type alias is to make our code more readable, and clearer in context for human understanding.
Interfaces
Interfaces in TypeScript define a contract or a blueprint for object structures. They allow you to specify the names, types, and optional/required status of properties, as well as define methods that should be implemented by objects that adhere to the interface.
union type
Union types in TypeScript allow you to specify that a variable or parameter can hold values of different types. It provides flexibility by allowing a value to be of one type or another. Union types are denoted by using the pipe symbol (|) between the types.
Tuple Types
A tuple is a TypeScript type that works like an array with some special considerations:
The number of elements of the array is fixed.
The type of the elements is known.
The type of the elements of the array need not be the same.
Enums
Enums in TypeScript are used to define a set of named constants, typically representing a specific set of related values. They provide a way to create more expressive and self-documenting code by assigning meaningful names to values.
numeric enums
Numeric enums in TypeScript are a type of enum where each member has an associated numeric value. By default, the values start from 0 and increment by 1 for each subsequent member. However, you can explicitly assign numeric values to the members if desire
Array generics
Array generics in TypeScript allow you to define the type of elements that an array can hold. Generics provide a way to create reusable components or functions that can work with different types while maintaining type safety.
By using array generics, you can specify the type of elements that an array will contain, ensuring that only values of that specific type can be added to the array. This helps catch type errors at compile-time and provides better tooling support for autocompletion and type inference.
We can either use var or let for declaring an array.
The difference is var is used if we want the variable to be reflected/implemented throughout the program (global) and let for a specific block (local).
Typescript classes
TypeScript offers full support for the class keyword introduced in ES2015.
As with other JavaScript language features, TypeScript adds type annotations and other syntax to allow you to express relationships between classes and other types.
There are just a few differences between class constructor signatures and function signatures:
Constructors can’t have type parameters - these belong on the outer class declaration, which we’ll learn about later
Constructors can’t have return type annotations - the class instance type is always what’s returned
A function property on a class is called a method. Methods can use all the same type annotations as functions and constructors:
Other than the standard type annotations, TypeScript doesn’t add anything else new to methods.
Classes can also have accessors (getters/setters). TypeScript has some special inference rules for accessors:
If get exists but no set, the property is automatically readonly
If the type of the setter parameter is not specified, it is inferred from the return type of the getter
Getters and setters must have the same Member Visibility
Describe Generics and generic classes
Generics in programming languages, including TypeScript, provide a way to create reusable components or functions that can work with different types while maintaining type safety. Generics allow you to define placeholders for types that are specified when the component or function is used, enabling flexibility and code reusability.
In TypeScript, generics can be used in various contexts, including functions, interfaces, and classes. When applied to classes, they are known as generic classes.
Generic Classes:
A generic class in TypeScript is a class that can work with multiple types. It allows you to define a class with one or more type parameters, which act as placeholders for the actual types used when creating instances of the class.