Typescript Flashcards

1
Q

What is typescript and why I can’t learn it without knowing javascript?

A

Typescript is a superset of javascript to improve the development experience, therefore typescript is created on top of javascript.

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

What typescript compiles (aka transpile) to? Is it compiled to machine code? Is it readable?

A

to javascript. no. Yes, readable javascript

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

What are the three main benefits of typescript?

A

typing, organization (classes, namespaces, modules) and tooling.

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

If typescript is transpiled to js, will debug of js code (which actually runs on browser) be mapped to our typescript code?

A

yes.

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

What is the name of the typescript compiler? Will tsc command look for ts files in the current dir and subfolders?

A

tsc. yes

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

How to install typescript?

A

npm install -g Typescript

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

Can I configure tsc to build a specific target version of js?

A

yes.

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

Where are typescript configurations stored? How to generate one?

A

tsconfig.json. tsc –init

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

What is type inference in typescript?

A

typescript infers the type of vars when not specified;

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

does typescript support access modifiers in classes? What are the three of them? What is the default when not explicitly defined?

A

public, protected and private. public.

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

does the tsconfig.json file support configuration inheritance (meaning root configs can be overwritten by lower level folders)? What does the option extends do in the tsconfig file?

A

yes. define the “base” tsconfig file.

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

what is the option “watch”: true means in the tsconfig file?

A

changes to tsconfig files are automatically compiled to js

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

What happens when the compiler doesn’t find a tsconfig.json file in the current dir?

A

It will go up in the folder hierarchy looking for one

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

What does the strict: true option in the tsconfig.json file does?

A

enable all strict options to true (the most strict option of all).

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

does webpack support compiling typescript?

A

yes. via ts-loader

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

does the bundle.js generated by webpack available locally?

A

no. just served to the browser (via webpack developer server)

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

what is the number type represents in typescript and javascript?

A

float point values

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

What’s the one primitive type exclusively available in typescript?

A

enum

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

Does typescript support hoisting? What does it mean?

A

Yes. Means the is run once to load all variables and then executed again to actually run the code.

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

If type is inferred on typescript why would I annotate the types in my code?

A

to give code clarity.

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

does let support hoisting? which one does?

A

no. only var.

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

What is the Never built-in type?

A

Is a type that means the function is an infinite loop or always throw an exception.

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

When the type Any is useful?

A

Useful when integrating third party js libs.

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

What is union type? how to declare one?

A

means a variable can have two or more types. Declared as let asd: number | string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is type assertion? how is it called in Delphi/C#?
means you can cast to a type. typecast
26
What are the two ways to do a type assertion on typescript?
myVar | myVar as number
27
do I need to restart npm/webpack server when the tsconfig.json file is changed?
yes.
28
how to check the type of a variable?
typeof myvar === 'string'
29
how to declare an optional parameter in a function? Where this param needs to be?
function fun(a: string, b?:string). Needs to be at the end.
30
What is implicit return and when it doesn't work?
Implicit return works in one line arrow functions (aka anonymous / lambda). Doesn't work in multi line methods.
31
Can I have an arrow function with one annotated parameter without parenthesis?
no, parenthesis is required.
32
Can I create a variable that accepts a function? how?
let func: (p: string) => void;
33
What is the "Structural Type System" in typescript?
Means that anonymous objects that has the required structure (can have more data) can be used as a different type.
34
What happens when you annotate a variable with the question mark?
Makes it nullable by making an union type of the annotated type | undefined.
35
how to declare a static method in a class?
add static keyword.
36
how to declare a constructor? What do I have to call first when it extends another class?
constructor () { super(); } need to call super.
37
when a readonly class propery can be defined? how many times can it change?
only in the declaration itself or in the constructor. can only be set once.
38
how to declare a property in the constructor?
by adding public to the property: constructor(public s: string) {}
39
Why creating modules on typescript?
Higher level abstraction (think of larger building blocks). Also reusability and encapsulation.
40
can you define different module format via tsconfig.json? What is the default?
yes, ES2015.
41
What happens when the browser does not support modules?
you need to use a bundler, such a webpack (there are several of them).
42
How to make a type or function available to be used in other modules?
via export keyword
43
How to make a list of types and/or functions available to other modules?
export {func, mytype, myintf as intf123}
44
How to make a type/func private to the module?
just don't export it.
45
What are the four ways to import functionality from a module?
import {items...} from './asd' import defaultExportedWithAlias from './asd' import {item as asd} from './asd' import * as asd from './asd'
46
What is the difference between relative reference and non-relative reference to modules? When each of these is used the most?
relative reference is a simple relative path, the other doesn't have a path at all. The first on our own modules, the second on third party modules.
47
What is the two module resolution strategies, which one is simpler and come with ES2015 module format by default? Which one is more configurable.
specifies how modules are find. Classic is simpler (less configurable) and Node (more configurable) is more complex. Node.
48
How does the Classic Module Resolution Strategy works?
just look for a file with the ts or d.ts extensions (you specify the path in the import). It goes from current to up in the directories when a relative path is not specified.
49
How does the Node Module Resolution Strategy works?
looks in the current dir, then /importName/package.json then goes to index.ts, d.ts, or tsx files. The same for non-relative imports however it goes directly to the node modules folder.
50
What happens in Non-relative path Node Module Resolution Strategy when a node folder is not found?
it goes up until it finds a node folder.
51
Can I define a flag in the compiler so that it shows the pathes it goes until it finds the file? What is its name?
yes. traceResoltion: true in the tsconfig.json
52
How to make tsc look into a different folder other than the node_modules when looking for non-relative path node modules? are there other ways to change this path?
by changing the baseUrl: ./modules in the tsconfig.json. yes, many
53
What is a Type Declaration File? What is the extension of these files?
It is a TypeScript wrapper for javascript libraries (so you can make use of types / signatures). .d.ts
54
What is the best github repo to download the Type Declaration Files?
DefinetlyTyped repo.
55
How to install a DefinetelyTyped type via npm?
npm install --save @types/
56
How do I search types available in DefinetelyTyped repo?
microsoft.github.io/TypeSearch
57
What is a higher order function?
Higher-order functions are functions that operate on other functions. Either by receiving them as arguments or returning them as values.
58
A function that receives a function by argument is called?
Higher-order function
59
What is a mock.fn()?
A higher-order function, meaning it can receive a function as a parameter.
60
What is an Array['asd'] what concept does it use?
array of strings, it uses generics.
61
does type inference work with generic types (e.g: function that returns T)
yes.
62
does function without class also support generics?
yes, function asd(item: T): T
63
Can I have more than one generic type?
yes, just coma separate them:
64
How to apply type constraints to a generic type of T? What does it do? Do I get new properties?
asd or interface. Only MyClass or inherited objects can be used here. You can then use properties of MyClass.
65
Can I have a static method in a generic class?
no. because generics only works over instances of classes (or single methods).
66
What is the problem that decorators solve?
Cross-cutting concerns that aren't easily solved by inheritance (or maybe other oop concepts).
67
What is cross cutting concern? give an example.
Is a functionality that is very likely to be used in all controllers and/or codebase. Logging. https://pasteboard.co/JPeNPay.png
68
What is Aspect-Oriented Programming (AOP)? What implements this paradigm?
Is a paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. The decorator pattern implements this paradigm.
69
What is the difference between Decorators and Annotations?
Annotations are limited to setting metadata and decorators are functions that can modify what they described when executed.
70
What are the four types of decorators?
Class, method, property and parameter.
71
What is the order of evaluation followed when multiple decorators are defined? What about the actual functions, when are they called? Why?
Top to bottom. bottom to top. Because they(are(nested(functions())))
72
How to declare a class decorator?
by implementing a function that returns ClassDecorator.
73
What are the two types of decorators?
Action and Descriptor decorations.
74
What is a nice way to create a db that is actually a simple json file?
By using JsonDB.