Typescript 4 (GPT) Flashcards

1
Q

What is TypeScript?

A

TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main benefits of using TypeScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you install TypeScript?

A

You can install TypeScript globally using npm by running the command: npm install -g typescript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you compile TypeScript code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is static typing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is dynamic typing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a type annotation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is type inference?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the basic data types in TypeScript?

A

The basic data types in TypeScript include number, string, boolean, null, undefined, object, and symbol.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you declare a variable with a specific type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a union type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an intersection type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are type guards?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is type assertion?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are generics?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you define a generic type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are interfaces in TypeScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you define an interface?

A

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; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is duck typing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are classes in TypeScript?

A

Classes in TypeScript provide a way to define blueprints for creating objects with properties and methods. They support inheritance, encapsulation, and polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you define a class in TypeScript?

A

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; } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is inheritance?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you implement inheritance in TypeScript?

A

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); } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is access modifier?

A

Access modifiers are keywords used to control the visibility and accessibility of class members (properties and methods). TypeScript supports three access modifiers: public, private, and protected.

25
Q

What is encapsulation?

A

Encapsulation is the bundling of data (properties) and methods that operate on that data within a single unit (class). It hides the internal state of an object and only exposes the necessary interfaces for interacting with it.

26
Q

What is the difference between public, private, and protected access modifiers?

A

Public members are accessible from outside the class, private members are accessible only within the class, and protected members are accessible within the class and its subclasses.

27
Q

What is a constructor?

A

A constructor is a special method in a class that is automatically called when an instance of the class is created. It is used to initialize object properties and perform other setup tasks.

28
Q

What is method overloading?

A

Method overloading is

a feature in TypeScript that allows a class to have multiple methods with the same name but different parameter types or a different number of parameters. This enables developers to create more flexible and expressive APIs.

29
Q

What is method overriding?

A

Method overriding is a feature in TypeScript that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. It is used to customize the behavior of inherited methods.

30
Q

What is a static member?

A

A static member is a class member (property or method) that belongs to the class itself rather than to instances of the class. It is accessed using the class name rather than an instance variable.

31
Q

What is a module?

A

A module is a way to organize code into separate files or units of functionality. TypeScript supports both CommonJS and ES6 module systems for defining and importing/exporting modules.

32
Q

What is a namespace?

A

A namespace is a way to organize code by grouping related types and functions under a common name. It helps prevent naming collisions and provides a logical structure for organizing code.

33
Q

How do you define a namespace in TypeScript?

A

You can define a namespace using the namespace keyword followed by the namespace name and a set of type and function declarations enclosed in curly braces. For example: namespace MyNamespace { export interface Person { name: string; age: number; } }

34
Q

What is a decorator?

A

A decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. It is used to modify the behavior of the decorated entity or to annotate metadata to it.

35
Q

How do you define a decorator?

A

You can define a decorator by creating a function that takes specific parameters depending on where it is applied (e.g., class, method, property) and returns a value or function that will be used to modify or annotate the target.

36
Q

What are the built-in decorators in TypeScript?

A

Some of the built-in decorators in TypeScript include @deprecated, @experimental, @override, @sealed, and @abstract.

37
Q

What is asynchronous programming?

A

Asynchronous programming is a programming paradigm that allows multiple operations to be performed concurrently, without blocking the execution of the main thread. It is commonly used for tasks such as fetching data from a server or handling user input.

38
Q

How do you handle asynchronous code in TypeScript?

A

You can handle asynchronous code in TypeScript using promises, async/await syntax, or callback functions. Promises provide a way to represent asynchronous operations and handle their results or errors.

39
Q

What are async functions?

A

Async functions are functions declared with the async keyword, which enables them to use the await keyword inside. They allow you to write asynchronous code in a synchronous-like manner, making it easier to read and understand.

40
Q

What is the difference between null and undefined in TypeScript?

A

In TypeScript, null and undefined are both used to represent the absence of a value, but they have different semantics. null is explicitly assigned by developers to indicate a missing value, while undefined is the default value assigned to variables that have not been initialized.

41
Q

What is strict null checking?

A

Strict null checking is a TypeScript compiler option that enables stricter null checking by disallowing variables to be assigned null or undefined unless explicitly specified in their type declaration. It helps prevent null-related runtime errors.

42
Q

What is the keyof keyword?

A

The keyof keyword is a TypeScript operator that returns a union type consisting of the names of the properties of a given type. It is commonly used in conjunction with indexed access types and mapped types.

43
Q

What is the typeof keyword?

A

The typeof keyword is a TypeScript operator that returns the runtime type of a value as a string. It is often used in type guards and conditional type expressions to perform type checks at runtime.

44
Q

What are conditional types?

A

Conditional types are a feature of TypeScript that allows you to create types that depend on a condition. They are defined using the extends keyword followed by a ternary operator, enabling type transformations based on type relationships.

45
Q

What are mapped types?

A

Mapped types are a feature of TypeScript that allows you to create new types by transforming the properties of an existing type. They use the keyof and in operators to iterate over the keys of a type and apply transformations to each property.

46
Q

What is the key difference between interface and type aliases?

A

The key difference between interfaces and type aliases is that interfaces can be extended or implemented by other interfaces or classes, while type aliases cannot. Additionally, interfaces can be merged, allowing multiple declarations to contribute to a single interface definition.

47
Q

How do you create an alias for a union type?

A

You can create an alias for a union type using a type alias declaration. For example: type MyType = number | string; creates an alias MyType that represents a union of number and string types.

48
Q

What is a declaration merging?

A

Declaration merging is a feature of TypeScript that allows multiple declarations with the same name to be combined into a single merged declaration. It is commonly used with interfaces and namespaces to extend or augment existing definitions.

49
Q

What is ambient declaration?

A

Ambient declarations are declarations that specify the shape of an external entity (e.g., a library, module, or global variable) without providing an implementation. They are commonly used to provide type information for JavaScript libraries or browser APIs.

50
Q

How do you create an ambient declaration?

A

You can create an ambient declaration by using the declare keyword in TypeScript followed by the declaration of the external entity. For example: declare var jQuery: (selector: string) => any; declares an ambient variable jQuery representing the jQuery library.

51
Q

What is the triple-slash directive?

A

The triple-slash directive is a special syntax in TypeScript used to reference external files or dependencies. It is typically placed at the top of a file and begins with three consecutive slashes followed by a directive (e.g., /// ).”

52
Q

How do you use the triple-slash directive to reference a declaration file?

A

You can use the triple-slash directive to reference a declaration file (typically with a .d.ts extension) containing type definitions for external libraries or modules. For example: /// \ references a declaration file for the jQuery library.”

53
Q

What is the keyof operator used for?

A

The keyof operator in TypeScript is used to obtain the keys of an object type as a union of string literal types. It is often used in conjunction with indexed access types and mapped types to perform type-safe property access and manipulation.

54
Q

What is the difference between keyof and Object.keys()?

A

The keyof operator in TypeScript operates at the type level and returns a union of string literal types representing the keys of an object type. In contrast, Object.keys() is a runtime function in JavaScript that returns an array of strings representing the enumerable property names of an object.

55
Q

What is a type assertion in TypeScript?

A

A type assertion in TypeScript is a way for a developer to tell the compiler about the type of a value, overriding the default type inference. It is similar to type casting in other programming languages.

56
Q

What is the as keyword used for in TypeScript?

A

The as keyword in TypeScript is used for type assertion, allowing a developer to specify the type of a value when the compiler cannot infer it automatically. It is commonly used in conjunction with union types and type guards to perform type-specific operations.

57
Q

What is a type predicate?

A

A type predicate is a function that returns a boolean value, used to assert the type of its input parameter. It is typically used as a type guard in TypeScript to narrow down the type of a variable within a conditional block.

58
Q

What is a type guard?

A

A type guard is a construct in TypeScript used to narrow down the type

of a variable within a conditional block, based on runtime checks. It allows developers to write more robust and type-safe code when dealing with union types and type assertions.