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
Q

What is type assertion? how is it called in Delphi/C#?

A

means you can cast to a type. typecast

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

What are the two ways to do a type assertion on typescript?

A

myVar

myVar as number

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

do I need to restart npm/webpack server when the tsconfig.json file is changed?

A

yes.

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

how to check the type of a variable?

A

typeof myvar === ‘string’

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

how to declare an optional parameter in a function? Where this param needs to be?

A

function fun(a: string, b?:string). Needs to be at the end.

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

What is implicit return and when it doesn’t work?

A

Implicit return works in one line arrow functions (aka anonymous / lambda). Doesn’t work in multi line methods.

31
Q

Can I have an arrow function with one annotated parameter without parenthesis?

A

no, parenthesis is required.

32
Q

Can I create a variable that accepts a function? how?

A

let func: (p: string) => void;

33
Q

What is the “Structural Type System” in typescript?

A

Means that anonymous objects that has the required structure (can have more data) can be used as a different type.

34
Q

What happens when you annotate a variable with the question mark?

A

Makes it nullable by making an union type of the annotated type | undefined.

35
Q

how to declare a static method in a class?

A

add static keyword.

36
Q

how to declare a constructor? What do I have to call first when it extends another class?

A

constructor () {
super();
}
need to call super.

37
Q

when a readonly class propery can be defined? how many times can it change?

A

only in the declaration itself or in the constructor. can only be set once.

38
Q

how to declare a property in the constructor?

A

by adding public to the property: constructor(public s: string) {}

39
Q

Why creating modules on typescript?

A

Higher level abstraction (think of larger building blocks). Also reusability and encapsulation.

40
Q

can you define different module format via tsconfig.json? What is the default?

A

yes, ES2015.

41
Q

What happens when the browser does not support modules?

A

you need to use a bundler, such a webpack (there are several of them).

42
Q

How to make a type or function available to be used in other modules?

A

via export keyword

43
Q

How to make a list of types and/or functions available to other modules?

A

export {func, mytype, myintf as intf123}

44
Q

How to make a type/func private to the module?

A

just don’t export it.

45
Q

What are the four ways to import functionality from a module?

A

import {items…} from ‘./asd’
import defaultExportedWithAlias from ‘./asd’
import {item as asd} from ‘./asd’
import * as asd from ‘./asd’

46
Q

What is the difference between relative reference and non-relative reference to modules? When each of these is used the most?

A

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
Q

What is the two module resolution strategies, which one is simpler and come with ES2015 module format by default? Which one is more configurable.

A

specifies how modules are find. Classic is simpler (less configurable) and Node (more configurable) is more complex. Node.

48
Q

How does the Classic Module Resolution Strategy works?

A

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
Q

How does the Node Module Resolution Strategy works?

A

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
Q

What happens in Non-relative path Node Module Resolution Strategy when a node folder is not found?

A

it goes up until it finds a node folder.

51
Q

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?

A

yes. traceResoltion: true in the tsconfig.json

52
Q

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?

A

by changing the baseUrl: ./modules in the tsconfig.json. yes, many

53
Q

What is a Type Declaration File? What is the extension of these files?

A

It is a TypeScript wrapper for javascript libraries (so you can make use of types / signatures). .d.ts

54
Q

What is the best github repo to download the Type Declaration Files?

A

DefinetlyTyped repo.

55
Q

How to install a DefinetelyTyped type via npm?

A

npm install –save @types/

56
Q

How do I search types available in DefinetelyTyped repo?

A

microsoft.github.io/TypeSearch

57
Q

What is a higher order function?

A

Higher-order functions are functions that operate on other functions. Either by receiving them as arguments or returning them as values.

58
Q

A function that receives a function by argument is called?

A

Higher-order function

59
Q

What is a mock.fn()?

A

A higher-order function, meaning it can receive a function as a parameter.

60
Q

What is an Array[‘asd’] what concept does it use?

A

array of strings, it uses generics.

61
Q

does type inference work with generic types (e.g: function that returns T)

A

yes.

62
Q

does function without class also support generics?

A

yes, function asd(item: T): T

63
Q

Can I have more than one generic type?

A

yes, just coma separate them:

64
Q

How to apply type constraints to a generic type of T? What does it do? Do I get new properties?

A

asd or interface. Only MyClass or inherited objects can be used here. You can then use properties of MyClass.

65
Q

Can I have a static method in a generic class?

A

no. because generics only works over instances of classes (or single methods).

66
Q

What is the problem that decorators solve?

A

Cross-cutting concerns that aren’t easily solved by inheritance (or maybe other oop concepts).

67
Q

What is cross cutting concern? give an example.

A

Is a functionality that is very likely to be used in all controllers and/or codebase. Logging.
https://pasteboard.co/JPeNPay.png

68
Q

What is Aspect-Oriented Programming (AOP)? What implements this paradigm?

A

Is a paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. The decorator pattern implements this paradigm.

69
Q

What is the difference between Decorators and Annotations?

A

Annotations are limited to setting metadata and decorators are functions that can modify what they described when executed.

70
Q

What are the four types of decorators?

A

Class, method, property and parameter.

71
Q

What is the order of evaluation followed when multiple decorators are defined? What about the actual functions, when are they called? Why?

A

Top to bottom. bottom to top.

Because they(are(nested(functions())))

72
Q

How to declare a class decorator?

A

by implementing a function that returns ClassDecorator.

73
Q

What are the two types of decorators?

A

Action and Descriptor decorations.

74
Q

What is a nice way to create a db that is actually a simple json file?

A

By using JsonDB.