Skip to content
Snippets Groups Projects
Commit 61880a71 authored by Thibaut DE SAIVRE's avatar Thibaut DE SAIVRE
Browse files

finish typescript readme

parent f6cbb968
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,24 @@ When developping websites or backend applications, we use Node js. You can downl
Node js dependencies are managed using a package manager (npm, yarn, pnpm...). The default package manager is `npm`.
It comes bundled with Node js.
Node dependencies are installed in the `node_modules/` folder.
Every project has a `package.json` file, which contains the project's **dependencies**, **metadata** and **scripts** shorthands.
You can use the following commands :
```bash
npm install # Install all dependencies listed in package.json
npm install <package> # Install a package and add it to package.json
npm install <package> --save-dev # Install a package as a dev dependency
# Scripts we defined in package.json
npm run build # Build the project into javascript files. The output directory is build/
npm run execute # Execute the project. The entry point is build/index.js
npm run format # Run the prettier formatter on all files (this is done automatically upon saving each file)
npm run lint # Run the eslint linter on all files (this is done automatically by VSCode when opening or changing a file)
```
### Why use (or not) typescript ?
TypeScript is a strongly typed superset of JavaScript. It is recommended to ALWAYS use TypeScript for medium to large projects, especially if multiple developers will be working on it.
......@@ -72,100 +90,9 @@ You should always use a linter.
The linter is configured in the [`.eslintrc.js`](./.eslintrc.js) file.
## Environment and reproducibility
In order to make it easier for other developers to contribute to your project,
and to enable deploying your application easily, you must indicate which python version and dependencies you are using.
You typically indicate the python version in your [`pyproject.toml`](./pyproject.toml) or (`README.md`)(./README.md) file.
### Create a virtual environment
If you do not use `conda`, you may create a virtual environment. Virtual environments are often contained in a `venv` folder.
Virtual environment enable you to have separate python versions and dependencies for each project.
```bash
python --version # Ensure that your current python version is the one you want to use. Use `pyenv` to change it if needed (see wikibr).
python -m venv venv # Creates a virtual environment in the `venv` folder.
# LINUX / MACOS : activate the virtual environment
source venv/bin/activate
# WINDOWS : activate the virtual environment
venv\Scripts\activate
```
You must always activate your virtual environment before running your code or installing dependencies (else it will use your global python interpreter).
### Install and Freeze dependencies
Python depdendencies are typically indicated in a [`requirements.txt`](./requirements.txt) file.
Install dependencies :
```bash
pip install --upgrade pip # Update pip (python package manager)
pip install -r requirements.txt # Install dependencies read from the requirements file
```
Freeze dependencies :
You can write your dependencies by hand in the `requirements.txt` file, or you can "freeze" your current environment.
```bash
pip freeze > requirements.txt # Write the current dependencies to the requirements file
```
### Git
Some common gitignores for a python project :
- `__pycache__` : python cache, generated at runtime.
- `venv` : virtual environment folder.
## Python Clean Code
Some often-overlooked python clean code rules :
### Docstrings
Always write docstrings in order to describe the purpose of your functions and classes.
```python
def function():
"""This function does something."""
pass
```
### Type hinting
Some common gitignores for a TypeScript project :
When you can (always), use type hints to indicate the types of the parameters of your functions, their return types, etc...
You sometimes need to use the `typing` module to indicate more complex types.
```python
from typing import List
def add_to_list(my_list: List[int], element: int) -> List[int]:
my_list.append(element)
return my_list
```
Since python 3.10, you can use the `|` operator to indicate that a parameter can be of multiple types, and use `list` and `dict` directly as type hints.
```python
def add_to_list(my_list: list[int | float], element: int | float) -> list[int | float]:
my_list.append(element)
return my_list
```
### Use modules
When your code gets big, you should split it into multiple files.
Use the following test in order to run module-specific code only when you run your module directly via `python module.py`, and not when you import it from another file.
```python
def module_function():
pass
if __name__ == "__main__":
module_function()
```
- `node_modules/` : the `npm` dependencies.
- `build/` : the output build.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment