TypeScript Flashcards

1
Q

What are unions in TS?

A

Unions in TypeScript refer to a type annotation that allows a variable or parameter to have multiple possible types. A union type is denoted by using the vertical bar (|) between two or more types, indicating that a value can have any of those types.

function printValue(value: number | string) {
console.log(value);
}
printValue(3)
printvalue(“three”)

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

What are enums in TS?

A

Enums in TypeScript are a way to define a set of named values, where each value has a corresponding numeric or string value. Enums allow for the creation of a set of predefined constants with meaningful names, making code more readable and maintainable. Enums provide a way to represent a group of related values as a single named type in TypeScript.

enum Direction {
Up,
Down,
Left,
Right
}

let myDirection: Direction = Direction.Up;
console.log(myDirection); // prints 0

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

What are tuples in TS?

A

Tuples in TypeScript are a special type of array that allows for specifying the type and order of elements in the array. We can have fixed number of elements, where each element has a specific types.

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

What does type annotation mean in TS?

A

Type annotation in TypeScript refers to explicitly specifying the type of a variable, function parameter, or function return value using TypeScript’s type system. It allows you to explicitly define the expected type of a value or expression, which TypeScript will use to provide type checking and better static type inference during compilation.

Type annotations in TypeScript use a colon (:) followed by the type declaration to specify the type of a value. For example:

let myVar: number = 42; // Type annotation for a number variable
let myString: string = “Hello”; // Type annotation for a string variable
let myBool: boolean = true; // Type annotation for a boolean variable
or
function add(a: number, b: number): number { // Type annotations for function parameters and return value
return a + b;
}

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

What is interface?

A

In TypeScript, an interface is a way to define the structure or shape of an object. It describes the properties, methods, and types of values that an object must have in order to meet the requirements of the interface ( similar to type …)

interface Person {
name: string;
age: number;
address?: string; // Optional property
}

function greet(person: Person): void {
console.log(Hello, ${person.name}! You are ${person.age} years old.);
}

const john: Person = {
name: ‘John’,
age: 30
};

greet(john);

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

What does mean Strongly Type Language?

A

STL means that language demands specification of data types. TS is a statically typed language. This means types are checked at compile time. JS is dynamically typed language. This means typed are checked at a runtime. JS is loosely typed language aka “weakly typed”

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

What does infer or implicit mean?

A

If we don’t write data explicitly, TypeScript is going to infer type.

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

What is literal type in TS?

A

let user = ‘Max’ | ‘Arek’

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

What does void do in TypeScript?

A

Void type is used to indicate that a function does not return any value. It’s essentially the opposite of the any type, which can be anything.

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

What does type aliases refer to in TypeScript?

A

Type aliases are used to create new names for existing types. For example, if you have a union type string | number, you can create an alias for it:
type alphanumeric = string | number;

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

What is TypeScript?

A

TypeScript is statically typed language. Types are checked at the compile time.

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

What does type do in TypeScript?

A

type is a keyword used to define custom types. It allows you to create a new name for a type or a combination of types. You can use it to create complex types using unions, intersections, and tuples.

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

Differences between type and interface

A

Interfaces: Generally preferred for public API’s definition when authoring a library or 3rd party ambient type definitions due to their extensibility. Interface support “extends” keyword.
Types: Preferred for complex type transformations or when using types that cannot be expressed as an interface, such as unions or tuples.

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

What is type assertion?

A

type assertions - type of a value that TS can’t infer on its own. type One = string, and let d<One>"Max".
<> angle brackets are not usable in React file
example:
let someValue: any = "this is a string";</One>

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

What is index singature in TypeScript?

A

Is used in an interface or type to specify the types of possible values that can be used to index into an object,as well as the types of values that those indices can return. Useful when you when you wan tto declare a type that can have properties with names that are not known at design time.
interface StringArray {
[index: number] : string;
}

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