#node-js
## Prepare TypeScript in a project
- `npx tsc --init` creates a _tsconfig.json_ file
- If there is an error displayed in the _tsconfig.json_ file ("No inputs were found in config file"): This just means that no _.ts_ file is present yet in the project
## Documentation entry points
- https://www.typescriptlang.org
- [TSConfig reference](https://www.typescriptlang.org/tsconfig)
## Learnings
- Comments in _.json_ files are allowed in TypeScript projects
- [**@types/node**](https://www.npmjs.com/package/@types/node) needs to be installed when using native Node.js modules
- `type` versus `interface`: "[the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces)"
- Define a type of a function (which for example takes a string as input and returns a string): `type NameOfTheFunction = (argumentName: string) => string`
- It is possible to use `number[]` and `Array<number>` (also known as „generics“) in a type definition
- Define an array of arrays like so: `string[][]` (array of arrays of strings)
- Tuples (e.g. `[number, number]`) must contain the exact specified types of the exact specified amount. Do not confuse tuples with arrays: A tuple can contain elements of different types
- To enable a function to accept/return types in a flexible way, there is a concept of [generics](https://www.typescriptlang.org/docs/handbook/2/generics.html). When calling the function, the desired type needs to be provided. Here is an example where a function with a generic type is called with different types:
```javascript
function firstOf<T>(elements: Array<T>): T {
// Or use T[] instead of Array<T>
return elements[0];
}
firstOf<boolean>([true, false]); // true
firstOf<number>([10, 20]) // 10
```
- It is possible to assign different types in a generic:
```javascript
type Example<T1, T2> = {
value: T1,
anotherValue: T2
}
const example: Example<number, boolean> = {
value: 10,
anotherValue: true
}
```
- „Structural typing“ is also called duck typing. It means to only care about the properties that need to be accessed (note the destructuring that is happening here):
```javascript
function getName({name}: {name: string}) {
return name;
}
```
- TypeScript itself is quite straightforward to learn. Complexity sometimes lies in its configuration
- [ts-node](https://www.npmjs.com/package/ts-node) can be hairy to work with, especially in combination with ES Modules
- Writing a dual package was an interesting experience. I first tried the approaches described in the following two articles…
- [Publishing Node modules with TypeScript and ES modules](https://blog.logrocket.com/publishing-node-modules-typescript-es-modules/)
- [How to Create a Hybrid NPM Module for ESM and CommonJS](https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html)
- … but then ended up with [a much simpler variant that is described in the official Node.js documentation](https://nodejs.org/api/packages.html)
## Helpful resources
- [Node.js reference architecture - TypeScript](https://github.com/nodeshift/nodejs-reference-architecture/blob/main/docs/development/typescript.md)