Typescript Flashcards

1
Q

Why can’t TypeScript Types affect the runtime performance of your code?

A

Because code generation is independent of the type system. The code that’s being run is native JavaScript.

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

Why you should avoid any type?

A

Because it effectively silences the type checker and TypeScript language services.

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

What is a never type?

A

It’s a TypeScript type with empty domain: no values are assignable to a variable with a never type.

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

What are union types?

A

Unions of sets of values, eg:

type AB = ‘A’ | ‘B’;

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

What is & operator in type context?

A

The & operator computes the intersection of two types

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

Equation that illustrates the difference between Union and Intersection of interfaces?

A

keyof (A&B) = (keyof A) | (keyof B)

keyof (A|B) = (keyof A) & (keyof B)

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

What is the type’s domain?

A

It’s a set of values assignable to it

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

How does typeof operator differ between type and value space?

A
  • In a type context, typeof takes a value and returns its TypeScript type.
  • In a value context, typeof is JavaScript’s runtime typeof operator. It returns a string containing the runtime type of the symbol.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why should you prefer type declarations to type assertions?

A

Type declaration verifies that the value conforms to the interface and throws an error otherwise. Type assertion does not.

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

What is “!” used as a suffix?

A

It is interpreted as an assertion that the value is non-null. It should be used only if you’re confident it’s in fact non-null.

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

When is it possible to use type assertions to convert between A and B types?

A

When A or B is a subset of the other.

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

Why isn’t it possible to set properties on primitive string values?

A

Because JS engine wraps it in String object on the fly to perform this operation and then throws this object away. This also applies to calling methods on primitives.

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

What’s excess property checking?

A

TypeScript feature that when you assign an object literal to a variable with a declared type, TypeScript makes sure it has the properties of that type and no others.

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