TypeScript Flashcards

1
Q

What is TypeScript, and how does it differ from JavaScript?

A

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds static types to the language, which can help catch errors during development and improve code quality. Unlike JavaScript, TypeScript requires a compilation step.

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

What are the benefits of using TypeScript?

A
  • Early error detection through static type checking
  • Improved code readability and maintainability
  • Enhanced IDE support with features like auto-completion and refactoring
  • The ability to use modern JavaScript features
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the different types of variable declarations in TypeScript (var, let, const)

A

var declares a variable with function scope, let declares a block-scoped variable, and const declares a block-scoped constant that cannot be reassigned. let and const are recommended over var to avoid scope-related issues.

var can be redeclared, but not forlet and const

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

What are the basic types in TypeScript?

A

The basic types include boolean, number, string, array, tuple, enum, any, void, null, undefined, never, object, ‘function’, ‘unknown’

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

Explain the concept of “type inference” in TypeScript.

A

Type inference is when TypeScript automatically determines the type of a variable based on its initial value. For example, let x = 3; infers x to be of type number.

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

What is a “union type” in TypeScript?

A

A union type allows a variable to be one of several types. It is defined using the | symbol.

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

What is the difference between interface and type aliases in TypeScript?

A

Both can be used to describe the shape of an object, but interface is more versatile as it allows for declaration merging and extending other interfaces. type aliases can be used for complex types like unions and intersections but cannot be reopened to add new properties.

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

How do you use generics in TypeScript?

A

Generics allow you to create reusable components. For example:
~~~
function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>("myString");
interface IProcessor<T> {
result:T;
process(a: T, b: T) => T;
}
~~~</T></string></T>

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

What is the unknown type in TypeScript?

A

The unknown type is a type-safe counterpart of any. It means that a value could be anything, but it must be checked before performing operations on it. For example:
~~~
let value: unknown;
value = “hello”;
if (typeof value === “string”) {
console.log(value.toUpperCase());
}
~~~

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

What is diff any and unknown?

A

Anything is assignable to unknown, but unknown isn’t assignable to anything but itself.

while anything is assignable to any and any can assign to anything.

Type Safety:
- any: No type safety. You can perform any operation on a variable of type any without any checks, which can lead to runtime errors.
- unknown: Type-safe. You must perform type checks or type assertions before using a variable of type unknown, ensuring safer code.

Use Cases:
- any: Use when you want to disable type checking. Typically used during gradual migration to TypeScript or when dealing with legacy code.
- unknown: Use when you want to represent a variable of unknown type while maintaining type safety.

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

Explain the difference between interface and abstract class.

A

An interface is a purely structural contract that does not provide any implementation, while an abstract class can provide both abstract methods (without implementation) and concrete methods (with implementation). Abstract classes can have state (fields), whereas interfaces cannot.

interface: Supports multiple inheritance. A class can implement multiple interfaces.
abstract class: Supports single inheritance. A class can only extend one abstract (or any other) class but can implement multiple interfaces.

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

What is the purpose of type assertion in TypeScript?

A

Type assertions allow you to override the inferred type of a variable when you know more about the variable’s type than TypeScript does. For example:
~~~
let someValue: unknown = “this is a string”;
let strLength: number = (someValue as string).length;
~~~

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

Explain the never type in TypeScript.

A

The never type represents values that never occur. It is typically used for functions that always throw an error or never return. For example:

     function error(message: string): never {
       throw new Error(message);
     }
    
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are decorators in TypeScript?

A

Decorators are special types of declarations that can be attached to classes, methods, accessors, properties, or parameters. They provide a way to add annotations and a meta-programming syntax. For example:

     function log(target: any, key: string) {
       console.log(`${key} was called`);
     }
     class Person {
       @log
       sayHello() {
         console.log("Hello");
       }
     }
    
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create and use a mapped type in TypeScript?

A

Mapped types allow you to create new types based on existing ones by transforming their properties. For example:
~~~
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
interface User {
name: string;
age: number;
}
const user: Readonly<User> = {
name: "Alice",
age: 30
};
~~~</User></T>

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

Explain conditional types in TypeScript.

A

Conditional types are a way to express logic that depends on types. For example:
~~~
type Message<T> = T extends string ? string : number;
let msg: Message<string>; // string
let num: Message<number>; // number
~~~</number></string></T>

17
Q

What is type narrowing in TypeScript?

A

Type narrowing is the process of refining a type to a more specific type within a specific scope. It is typically achieved using type guards, such as typeof, instanceof, and custom type guard functions. For example:
~~~
function isString(value: any): value is string {
return typeof value === “string”;
}
let value: string | number;
if (isString(value)) {
console.log(value.toUpperCase());
} else {
console.log(value.toFixed(2));
}
~~~

18
Q

How do you use the Partial utility type in TypeScript?

A

The Partial utility type makes all properties of a type optional. For example:
~~~
interface User {
name: string;
age: number;
}
~~~

19
Q

What is diff enum and const enum ?

A
  • Purpose: const enum is used to define a set of named constants with the same functionality as enum, but optimized for performance. const enum values are inlined at compile time.
  • Compilation: const enum values are completely removed from the output JavaScript. The TypeScript compiler inlines the values directly wherever they are used. This results in no runtime representation and can lead to more efficient code.