Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #341: Merge TypeScript-tests page into Guide-on-defining-types. #412

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions Guide-on-defining-types.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Table of contents
- [Why do we need to define types?](#why-do-we-need-to-define-types-)
- [Introduction](#introduction)
- [Running the TypeScript tests](#running-the-typescript-tests)
- [Where should the types be defined?](#where-should-the-types-be-defined-)
* [Functions](#functions)
* [Classes](#classes)
Expand All @@ -18,12 +19,54 @@
* [Example](#example)
- [Cases encountered in Codebase:](#cases-encountered-in-oppia-codebase)

## Why do we need to define types?
## Introduction

We use a lot of [TypeScript](https://www.typescriptlang.org/) code at Oppia, which lets us write JavaScript code that is strongly typed. We declare the types of variables, function arguments, and function return values. Then, TypeScript checks that there aren't any type errors when it compiles our code. For example, if you have a function that expects an int, TypeScript won't let you call the function with a string.

Defining types helps with:
- Earlier detection of errors which in turn speeds development
- No run-time penalty for determining the type.
- Clean code. This allows developers to write more robust code and maintain it, resulting in better, cleaner code.
- Makes it easier for fellow developers to understand and use the already written code.

This page describes how to run the TypeScript tests to make sure the types are correct, and also explains how to specify types for existing code.

## Running the TypeScript Tests

You can run the TypeScript tests like this:

Python:
```console
python -m scripts.run_typescript_checks --strict_checks
```

Docker:
```console
make run_tests.typescript
```

These tests compile all ts files in the codebase and check for errors (including type errors) during compilation. The compiled files are put in the folder `local_compiled_js_for_test`, which is automatically deleted after the tests finish running. Note that this folder might not be deleted if you abort the tests early.

The tests pass if, at the end of the test output, you see the message:

```text
Compilation successful!
```

If the tests fail, you'll see:

```text
Errors found during compilation
```

Below this message will be errors describing what went wrong. For example, consider this error:

```text
core/templates/services/exploration-features-backend-api.service.ts(49,7): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'.
```

This means that on line 49 of `exploration-features-backend-api.service.ts`, you are passing a boolean to a function that expects a string.

## Where should the types be defined?
> NOTE: These conventions apply to the files in Angular.

Expand Down
46 changes: 0 additions & 46 deletions TypeScript-tests.md

This file was deleted.

3 changes: 1 addition & 2 deletions _Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@
* [[Oppiabot|Oppiabot]]
* [[Git cheat sheet|Git-cheat-sheet]]
* Frontend
* [[How to write frontend type definitions|Guide-on-defining-types]]
* [[TypeScript tests|TypeScript-tests]]
* [[How to define frontend types and test them|Guide-on-defining-types]]
* [[Frontend file naming conventions|The-File-Naming-Convention-and-Directory-Structure]]
* [[Angular Migration|Angular-Migration]]
* [[UX guidelines|Oppia-UX-guidelines-&-rationales]]
Expand Down
Loading