TypeScript Flashcards

Category sponsor

TypeScript is a statically typed superset of JavaScript created by Anders Hejlsberg at Microsoft. It is a language designed to increase productivity and improve code quality in large applications. TypeScript is characterized by a rich type system and advanced language features, enabling error detection at compile time and better support for development tools. This language offers features like interfaces, generics, and decorators, providing developers with tools to create more readable and maintainable code. TypeScript also supports full compatibility with JavaScript and gradual adoption in existing projects, maintaining performance and enabling easy use of the JavaScript ecosystem.

Our flashcard app includes carefully selected TypeScript interview questions with comprehensive answers that will effectively prepare you for any interview requiring TypeScript knowledge. IT Flashcards is not just a tool for job seekers - it's a great way to reinforce and test your knowledge, regardless of your current career plans. Regular use of the app will help you stay up-to-date with the latest TypeScript trends and keep your skills at a high level.

Sample TypeScript flashcards from our app

Download our app from the App Store or Google Play to get more free flashcards or subscribe for access to all flashcards.

What is the 'any' type in TypeScript and when is its use recommended?

The type any in TypeScript is a dynamic type that can represent any data type. Using the any type allows us to avoid type checking during compilation, and it can be treated similarly to the dynamic type in other programming languages, such as JavaScript.

Here's an example of using the any type in TypeScript:
let dynamicValue: any = 100;
dynamicValue = "hello";
dynamicValue = false;

Although using the any type is convenient, it is not recommended if we know the data structure. The main advantage of TypeScript over JavaScript is that TypeScript introduces static typing, which helps in detecting errors during compilation.

The use of the any type should therefore be limited to situations where we are unable to determine a specific type. For example, it may be necessary when dealing with dynamic data, such as API data, which can change its structure. Note - excessive use of the any type may lead to losing the benefits provided by TypeScript through type checking and verification.

Explain what the 'void' type is in the context of functions in TypeScript.

The void type in TypeScript is a special type returned by functions that do not return a value. In JavaScript / TypeScript terminology, functions that do not have a declared return type or do not have a return statement, return undefined. Such a function type can be declared using the void type.

The void type is useful when we want to create a function that is supposed to perform a specific action, but does not return a value. This is often used for functions aimed at changing the state of the application or displaying effects on the screen, such as reactions to click events.

Example:
function logMessage(message: string): void {
  console.log(message);
}

The logMessage function takes a string as an argument and displays it on the console. It does not return any value. The void type precisely defines that it should not return any value. If we try to return any value from such a function, TypeScript will report a compilation error.

What are the differences between an interface and a type in TypeScript?

In TypeScript, both interfaces and types serve similar purposes, but there are differences between them. I’ll outline a few below.

1. Ability to declare types in multiple ways: Types allow more complex declarations, for example, we can declare a type as a union, intersection, tuple type, literal types, index types, mapped types, conditional types, etc. Interfaces do not possess such flexibility.

2. Extending and implementing: Interfaces can be extended and implemented for classes, which may ease code writing. Types, by contrast, do not provide such functionality.

3. Declaration merging: Interfaces can have multiple declarations in one scope. All these declarations are merged into one interface. Types can not be declared more than once.

Example of interface merging:
interface User {
  name: string;
}

interface User {
  age: number;
}

// Now the User interface includes fields name and age

Example of a difference in declaring a type:
type User = {
  name: string;
};

// The compiler will generate an error because types cannot be declared more than once
type User = {
  age: number;
};

4. Structural type compatibility: Interfaces can be used for determining structural type compatibility, whereas types are designed to handle composite types.

Examples of using interfaces and types:
interface ExampleInterface {
    prop1: string;
}

type ExampleType = {
    prop1: string;
    prop2: number;
} | string | number;

But keep in mind that the choice between interface and type mainly depends on the axis or situation. Each has its pluses and minuses.

How can you define an object with specific properties and types in TypeScript?

In TypeScript, we can define an object with specified properties and types using so-called interface or type alias. Both of these mechanisms focus on checking the shape that a given object presents. They serve to describe the shape of objects. Here's how to do it:

1. Using Interface:
interface Person {
    firstName: string;
    lastName: string;
    age: number;
    isEmployed: boolean;
}

let john: Person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 25,
    isEmployed: true
};

2. Using Type Alias:
type Car = {
    make: string;
    model: string;
    year: number;
}

let myCar: Car = {
    make: 'Toyota',
    model: 'Corolla',
    year: 2020
};

In both cases, if we want to assign an object to a variable that does not agree with the declared shape (eg a field is missing or an extra one), we will get a compilation error.

Download IT Flashcards App Now

Expand your TypeScript knowledge with our flashcards.
From basic programming principles to mastering advanced technologies, IT Flashcards is your passport to IT excellence.
Download now and unlock your potential in today's competitive tech landscape.