Typescript Flashcards
What is typescript and why I can’t learn it without knowing javascript?
Typescript is a superset of javascript to improve the development experience, therefore typescript is created on top of javascript.
What typescript compiles (aka transpile) to? Is it compiled to machine code? Is it readable?
to javascript. no. Yes, readable javascript
What are the three main benefits of typescript?
typing, organization (classes, namespaces, modules) and tooling.
If typescript is transpiled to js, will debug of js code (which actually runs on browser) be mapped to our typescript code?
yes.
What is the name of the typescript compiler? Will tsc command look for ts files in the current dir and subfolders?
tsc. yes
How to install typescript?
npm install -g Typescript
Can I configure tsc to build a specific target version of js?
yes.
Where are typescript configurations stored? How to generate one?
tsconfig.json. tsc –init
What is type inference in typescript?
typescript infers the type of vars when not specified;
does typescript support access modifiers in classes? What are the three of them? What is the default when not explicitly defined?
public, protected and private. public.
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?
yes. define the “base” tsconfig file.
what is the option “watch”: true means in the tsconfig file?
changes to tsconfig files are automatically compiled to js
What happens when the compiler doesn’t find a tsconfig.json file in the current dir?
It will go up in the folder hierarchy looking for one
What does the strict: true option in the tsconfig.json file does?
enable all strict options to true (the most strict option of all).
does webpack support compiling typescript?
yes. via ts-loader
does the bundle.js generated by webpack available locally?
no. just served to the browser (via webpack developer server)
what is the number type represents in typescript and javascript?
float point values
What’s the one primitive type exclusively available in typescript?
enum
Does typescript support hoisting? What does it mean?
Yes. Means the is run once to load all variables and then executed again to actually run the code.
If type is inferred on typescript why would I annotate the types in my code?
to give code clarity.
does let support hoisting? which one does?
no. only var.
What is the Never built-in type?
Is a type that means the function is an infinite loop or always throw an exception.
When the type Any is useful?
Useful when integrating third party js libs.
What is union type? how to declare one?
means a variable can have two or more types. Declared as let asd: number | string
What is type assertion? how is it called in Delphi/C#?
means you can cast to a type. typecast
What are the two ways to do a type assertion on typescript?
myVar
myVar as number
do I need to restart npm/webpack server when the tsconfig.json file is changed?
yes.
how to check the type of a variable?
typeof myvar === ‘string’
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.
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.
Can I have an arrow function with one annotated parameter without parenthesis?
no, parenthesis is required.
Can I create a variable that accepts a function? how?
let func: (p: string) => void;
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.
What happens when you annotate a variable with the question mark?
Makes it nullable by making an union type of the annotated type | undefined.
how to declare a static method in a class?
add static keyword.
how to declare a constructor? What do I have to call first when it extends another class?
constructor () {
super();
}
need to call super.
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.
how to declare a property in the constructor?
by adding public to the property: constructor(public s: string) {}
Why creating modules on typescript?
Higher level abstraction (think of larger building blocks). Also reusability and encapsulation.
can you define different module format via tsconfig.json? What is the default?
yes, ES2015.
What happens when the browser does not support modules?
you need to use a bundler, such a webpack (there are several of them).
How to make a type or function available to be used in other modules?
via export keyword
How to make a list of types and/or functions available to other modules?
export {func, mytype, myintf as intf123}
How to make a type/func private to the module?
just don’t export it.
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’
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.
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.
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.
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.
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.
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
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
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
What is the best github repo to download the Type Declaration Files?
DefinetlyTyped repo.
How to install a DefinetelyTyped type via npm?
npm install –save @types/
How do I search types available in DefinetelyTyped repo?
microsoft.github.io/TypeSearch
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.
A function that receives a function by argument is called?
Higher-order function
What is a mock.fn()?
A higher-order function, meaning it can receive a function as a parameter.
What is an Array[‘asd’] what concept does it use?
array of strings, it uses generics.
does type inference work with generic types (e.g: function that returns T)
yes.
does function without class also support generics?
yes, function asd(item: T): T
Can I have more than one generic type?
yes, just coma separate them:
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.
Can I have a static method in a generic class?
no. because generics only works over instances of classes (or single methods).
What is the problem that decorators solve?
Cross-cutting concerns that aren’t easily solved by inheritance (or maybe other oop concepts).
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
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.
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.
What are the four types of decorators?
Class, method, property and parameter.
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())))
How to declare a class decorator?
by implementing a function that returns ClassDecorator.
What are the two types of decorators?
Action and Descriptor decorations.
What is a nice way to create a db that is actually a simple json file?
By using JsonDB.