Typescript 4 (GPT) Flashcards
What is TypeScript?
TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript.
What are the main benefits of using TypeScript?
Some of the main benefits of using TypeScript include static type checking, enhanced code maintainability and scalability, better IDE support, and improved code documentation.
How can you install TypeScript?
You can install TypeScript globally using npm by running the command: npm install -g typescript.
How do you compile TypeScript code?
To compile TypeScript code, you can use the TypeScript compiler (tsc) by running the command: tsc filename.ts. This will generate a JavaScript file with the same name as the TypeScript file.
What is static typing?
Static typing is a feature of TypeScript that allows variables, function parameters, and return types to be explicitly declared with a specific type, which is checked at compile time.
What is dynamic typing?
Dynamic typing is a feature of JavaScript where variables can hold values of any type, and type checking is performed at runtime rather than at compile time.
What is a type annotation?
A type annotation is a way to explicitly specify the type of a variable, function parameter, or return value in TypeScript using a colon followed by the desired type.
What is type inference?
Type inference is a feature of TypeScript that automatically deduces the type of a variable based on its initialization value, function return type based on the return statement, and function parameter types based on the context.
What are the basic data types in TypeScript?
The basic data types in TypeScript include number, string, boolean, null, undefined, object, and symbol.
How do you declare a variable with a specific type?
You can declare a variable with a specific type using a type annotation. For example: let num: number = 10; declares a variable num of type number with an initial value of 10.
What is a union type?
A union type allows a variable to hold values of multiple types. It is denoted by the pipe (|) symbol between the type names. For example: let val: number | string; declares a variable val that can hold values of either number or string type.
What is an intersection type?
An intersection type combines multiple types into one. It is denoted by the ampersand (&) symbol between the type names. For example: type A = { x: number }; type B = { y: number }; type C = A & B; creates a type C that has properties of both A and B.
What are type guards?
Type guards are expressions that help TypeScript narrow down the type of a variable within a conditional block, based on runtime checks. They are commonly used with union types to perform type-specific operations.
What is type assertion?
Type assertion is a way to tell the TypeScript compiler that you know more about the type of a value than it does. It is like type casting in other languages and is done using the as keyword or angle-bracket syntax (<>) followed by the target type.
What are generics?
Generics allow you to create reusable components and functions that can work with a variety of data types. They enable you to parameterize types in a class or function definition.
How do you define a generic type?
You can define a generic type by placing a type parameter in angle brackets (<>) after the name of the class or function. For example: function identity(arg: T): T { return arg; } defines a generic function identity that returns the same type as its argument.
What are interfaces in TypeScript?
Interfaces in TypeScript are used to define the structure of objects. They specify properties and their types, as well as method signatures, but do not provide an implementation.
How do you define an interface?
You can define an interface using the interface keyword followed by the interface name and a set of property and method declarations enclosed in curly braces. For example: interface Person { name: string; age: number; }
What is duck typing?
Duck typing is a typing system used in TypeScript where the suitability of an object for a particular operation is determined by the presence of certain properties or methods rather than its explicit type or class.
What are classes in TypeScript?
Classes in TypeScript provide a way to define blueprints for creating objects with properties and methods. They support inheritance, encapsulation, and polymorphism.
How do you define a class in TypeScript?
You can define a class using the class keyword followed by the class name and a set of property and method declarations enclosed in curly braces. For example: class Rectangle { width: number; height: number; constructor(w: number, h: number) { this.width = w; this.height = h; } }
What is inheritance?
Inheritance is a mechanism in object-oriented programming where a class (subclass) can inherit properties and methods from another class (superclass). This promotes code reuse and allows for the creation of hierarchical relationships between classes.
How do you implement inheritance in TypeScript?
You can implement inheritance in TypeScript using the extends keyword to indicate that a class inherits from another class. For example: class Square extends Rectangle { constructor(side: number) { super(side, side); } }