Skip to content

chore: add English version (#321) #322

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .languagetool.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disabledRules=EN_COMPOUNDS_PRE_CLASS,ENGLISH_WORD_REPEAT_RULE,EN_UNPAIRED_QUOTES,LC_AFTER_PERIOD[1],ID_CASING[2]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# exercises-typescript

[![Github Actions Status](../../workflows/Docker/badge.svg)](../../actions)
[![GitHub Actions Status](../../workflows/Docker/badge.svg)](../../actions)

## How to contribute

Expand Down
24 changes: 24 additions & 0 deletions description.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: |
TypeScript course: free training for developers
header: Typescript
description: |
In modern development, TypeScript has not just taken a firm place, but has replaced JavaScript in many places. Knowledge of TypeScript has become essential for any developer who works with either Node.js or the browser
seo_description: |
Sharpen your knowledge in a free Typescript course | Interactive exercises right in your browser | Free TypeScript course from CodeBasics

keywords:
- typescript
- typoscript
- type script
- тайпскрипт
- ts
- тс
- тайп скрипт
- онлайн курс
- бесплатный курс
- программирование
- code basics
- online course
- free course
- programming
5 changes: 5 additions & 0 deletions modules/10-basics/10-hello-world/en/EXERCISE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copy the exact code from the instructions into the editor and execute it by clicking “Run”.

```typescript
console.log('Hello, World!');
```
16 changes: 16 additions & 0 deletions modules/10-basics/10-hello-world/en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

As is tradition, we'll start by writing a 'Hello, World!' program. The program will print the following text:

<pre class='hexlet-basics-output'>
Hello, World!
</pre>

To print something, you need to give computer a special command. In TypeScript, we use console.log().

## Launch special features

The TypeScript compiler works slower than needed for code-basics exercises. Because of this, TypeScript errors in this course are only shown in the editor itself - highlighted in red and revealed when you hover.

During the exercise execution, only JavaScript errors are shown. This means that you should make sure that there are no red underscores in the editor before checking the code.

If you want to test the examples on your computer for correct behavior, make sure you use the `--strict` flag.
9 changes: 9 additions & 0 deletions modules/10-basics/10-hello-world/en/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
name: Hello, World!
tips:
- |
[Repl for experimenting with TypeScript](https://www.typescriptlang.org/play)
- |
[strict in tsconfig](https://www.typescriptlang.org/tsconfig#strict)
- >
[TypeScript compiler options](https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Using the sum example from the lesson, write a function that finds the product of the given numbers:

```typescript
multiply(3, 8); // 24
multiply(1, 2); // 2
```
124 changes: 124 additions & 0 deletions modules/10-basics/20-typescript-as-a-second-language/en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

TypeScript is given as a second language in our project. Therefore, you should know JavaScript to better understand the course material.

You should also have an understanding of data types, variables, conditional constructs, loops, functions, object properties and methods, and anonymous or lambda functions.

Learning a second programming language is easier than learning a first one, so the structure of the material changes a lot. Here we review the basic constructs to quickly familiarize ourselves with the syntax. Then we move on to the tasks for which TypeScript is studied.

In this tutorial, we'll learn what TypeScript is, as well as break down its features, installation, and getting it up and running.

## What is TypeScript

TypeScript is JavaScript with an additional syntax for specifying data types. The first one refers to statically typed languages, the second one to dynamically typed languages.

To learn more about TypeScript, let's compare it to JavaScript.

Dynamically typed languages like JavaScript have an interpreter, a program that executes code line by line without prior analysis:

```bash
# node (Node.js) — JavaScript interpreter
# The code is immediately launched for execution
node index.js
```

If there are type errors in such a code, for example, a number of `BigInt` type came into the function instead of a regular number, we will learn about the error only when we run the code:

```javascript
function sum(a, b) {
return a + b;
}

sum(10n, 5); // oops
// 10n is a number of BigInt type, adding it to a regular number will cause a runtime error
// Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
```

Statically typed languages like TypeScript work differently. They have a number of advantages:

* Finding some types of errors even before the code is executed
* Easier code refactoring
* Full support for editor features: autocomplete add-ons, code navigation, etc.

Before you can run code in TypeScript for execution, you must compile it.

During compilation, it is checked that the program is **type-safe** - it does not contain errors like the example above. If the compiler finds a type mismatch, it stops compilation and displays warnings about where the types do not match.

Same example in TypeScript:

```typescript
function sum(a: number, b: number) {
return a + b;
}

sum(10n, 5);
// An error will occur at the compile stage
// Argument of type 'bigint' is not assignable to parameter of type 'number'.
```

Not only will the code above fail to run, but it will also fail to compile. The TypeScript compiler will stop execution and indicate a type mismatch error.

If we look at the function definition, we can see that we are facing almost the same JavaScript, except for the type description of input variables in the function.
It says that parameters `a` and `b` have type `number`. The rest of the TypeScript code is annotated with types in much the same way. The type descriptions can be very complex in places, but the idea remains the same.

We cannot say unequivocally which approach is better - static or dynamic typing. It all depends on the specific situation, and successful projects can be written in different languages. Since we are studying TypeScript in this
course, we will look at the peculiarities of working with it.

## TypeScript Features

TypeScript has become one of the most popular typed languages due to the following features:

* TypeScript is almost completely compatible with JavaScript in terms of features and types. Everything written in
TypeScript is also available in JavaScript, and vice versa. TypeScript is called a superset of the JavaScript
language. Meaning that it is the same JavaScript + type description.
* The TypeScript compiler turns TypeScript code into JavaScript code. That is, it removes type definitions from TypeScript code. This process is also known as **transpilation**.
* The developer of TypeScript is Microsoft
* Strong typing

Let's look at the last feature in more detail.

### Strong typing

Strong typing is not related to static typing. It refers to the extent to which a language allows automatic type conversions. A static language can be weak, and vice versa.

For example, in JavaScript we can add a number to a string. When the language encounters such a construct, it will automatically perform a type conversion: `10 + 'one'`.
TypeScript won't do that. It will generate an error: `The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type`.

Many dynamic languages are strongly typed. For example, Ruby and Python are among them. Strong typing only makes the language better and does not complicate the process of programming.

We've covered the advantages and features of TypeScript. Now you can learn how to install it.

## How to install and run TypeScript

TypeScript is written in TypeScript, which is then transpiled into JavaScript and distributed as a normal npm package. The installation of TypeScript is therefore very simple:

```bash
# Node.js have to be installed
# Inside the project, where TypeScript will be used
npm install typescript
```

Then you need to create TypeScript source files. These have the extension *ts*. The TypeScript compiler is available through the `tsc` utility:

```bash
npx tsc index.ts
```

The output is a file with JavaScript code that can already be executed with Node.js:

```bash
node index.js
```

You can also supply the package [ts-node](https://github.com/TypeStrong/ts-node), which compiles and runs at the same time. This package provides a REPL for experimenting with TypeScript.

## Conclusions

In this lesson, we learned about the TypeScript language. We learned that it is the same as JavaScript, but with additional syntax for specifying data types. TypeScript has several advantages over JavaScript:

* Finding some errors even before the code is executed
* Easier code refactoring
* Full support for editor features: autocomplete add-ons, code navigation, etc.

It is impossible to say which language is better. It all depends on the specific task and features of the language.

We also learned about the features of TypeScript in this lesson, and we also learned how to install and run it.
10 changes: 10 additions & 0 deletions modules/10-basics/20-typescript-as-a-second-language/en/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: TypeScript
tips:
- |
[Telegram Hexlet: JavaScript/TypeScript](https://t.me/hexletcommunity/12)
- >
[TypeScript boilerplate](https://github.com/hexlet-boilerplates/typescript-package)
- >
[Playground with the ability
to see the result of TypeScript to JavaScript transpilation](https://www.typescriptlang.org/play)
7 changes: 7 additions & 0 deletions modules/10-basics/30-variables/en/EXERCISE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Complete the body of the `repeat()` function that repeats the string the specified number of times. The function should return the result. Try not to use built-in methods, you will need a loop for this implementation.

```typescript
repeat('hexlet', 2); // hexlethexlet
repeat('wo', 3); // wowowo
```
92 changes: 92 additions & 0 deletions modules/10-basics/30-variables/en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

In this lesson we will learn how TypeScript and JavaScript differ in terms of working with variables. We will learn what type inference is and why it is necessary in programming. We'll also learn why TypeScript doesn't require you to manually specify the type of variables.

## Type inference

Variables and constants in TypeScript are defined in the same way as in JavaScript:

```typescript
let age = 10;

let company = 'Hexlet';
let user = {
firstName: 'Miro',
};
let fruits = ['apple', 'banana'];
```

In doing so, TypeScript does some extra work in the background. It automatically associates a variable or constant with the data type of the initial value. In programming, this process is called **type inference**.

The type of the variable cannot change:

```typescript
let age = 10;
// OK since it's the same type (Number)
age = 11.1;

// Type 'string' is not assignable to type 'number'.
age = 'some string'; // Error!
```

If we try to pass this variable to a method that expects a different type, that too will result in an error:

```typescript
// Argument of type 'number' is not assignable to parameter
// of type '(substring: string, ...args: any[])
'hexlet'.replace('xl', age);
```

Static typing imposes a restriction on arrays. Only data of one type can be stored inside:

```typescript
let items = [1, 2, 3];
items.push(4); // OK

// Argument of type 'string' is not assignable to parameter of type 'number'.
items.push('code-basics'); // Error!
```

With objects, the situation is even stricter. In TypeScript, you cannot not only change the type of properties inside an object, but also add new properties dynamically:

```typescript
let user = {
firstName: 'Miro',
};

// Property 'lastName' does not exist on type '{ firstName: string; }'.
user.lastName = 'Smith';
```

## Explicit type indication

TypeScript allows you to explicitly specify the type of variables. In practice, however, you rarely need to do this manually, since type inference works automatically:

```typescript
let name: string = 'Alice';
const count: number = 100;
let canPlay: boolean = true;
```

## Null

By default, in TypeScript variables can only contain the specified type with no exceptions, for example, we cannot assign null:

```typescript
let age = 30;
age = null; // Error!
```

This behavior protects us from many errors that are related to the fact that there are no checks for null. At the same time, `null` is sometimes an acceptable value. In this case, a special Union Type is used:

```typescript
let age: number | null = 30;
age = null;
```

Here we have specified that the type of the `age` variable is `number | null`. It is read as “number or null”.

Union Type is an interesting and handy concept that we'll look at in more detail later.

## Conclusions

In this lesson, we learned about variables in TypeScript. We learned how TypeScript differs from JavaScript in terms of working with variables. We also learned why you don't have to manually specify the type of variables in TypeScript.
9 changes: 9 additions & 0 deletions modules/10-basics/30-variables/en/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
name: Variables
tips:
- >
[Method
String.repeat()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/repeat)
- >
[Official
documentation](https://www.typescriptlang.org/docs/handbook/2/basic-types.html)
12 changes: 12 additions & 0 deletions modules/10-basics/40-named-functions/en/EXERCISE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

Implement the `getHiddenCard()` function. It takes as input a credit card number, which consists of 16 digits, as a string and returns a hidden version of it. This version can be used on the site for display. For example, if the original card number was *2034399002125581*, the hidden version looks like this: *\*\*\*\*5581*.

The function replaces the first 12 characters with asterisks. The number of asterisks is controlled by the second optional parameter. The default value is 4.

```typescript
// The credit card is passed as a string
getHiddenCard('1234567812345678', 2) // "**5678"
getHiddenCard('1234567812345678', 3) // "***5678"
getHiddenCard('1234567812345678') // "****5678"
getHiddenCard('2034399002121100', 1) // "*1100"
```
Loading