Skip to content

Commit 6a83ea2

Browse files
committed
complete ts chapter - 01
1 parent 6d40619 commit 6a83ea2

File tree

8 files changed

+143
-2
lines changed

8 files changed

+143
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
| Chapter | Details |
44
|:-------:|:-------:|
5-
| 1: Getting started with TypeScript | [Readme]() |
6-
| 2: Why and when to use TypeScript | [Readme]() |
5+
| 1: Getting started with TypeScript | [Readme](/chapters/1/readme.md) |
6+
| 2: Why and when to use TypeScript | [Readme](/chapters/2/readme.md) |
77
| 3: TypeScript Core Types | [Readme]() |
88
| 4: Arrays | [Readme]() |
99
| 5: Enums | [Readme]() |

chapters/1/codes/1.1/app.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
TypeScript is a typed superset of JavaScript that compiles directly to JavaScript code. TypeScript files commonly use the `.ts` extension. Many IDEs support TypeScript without any other setup required, but TypeScript can also be compiled with the `TypeScript Node.JS` package from the command line.
2+
3+
### Install the npm package globally
4+
You can install TypeScript globally to have access to it from any directory.
5+
- `npm install -g typescript`
6+
7+
### Install the npm package locally
8+
You can install TypeScript locally and save to `package.json` to restrict to a directory.
9+
npm install typescript --save-dev Installation channels
10+
11+
### You can install from:
12+
- Stable channel: `npm install typescript`
13+
- Beta channel: `npm install typescript@beta`
14+
- Dev channel: `npm install typescript@next`
15+
16+
### Compiling TypeScript code
17+
The tsc compilation command comes with typescript , which can be used to compile code.
18+
- `tsc my-code.ts`
19+
This creates a my-code.js file.

chapters/1/codes/1.2/app.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
TypeScript is a typed superset of JavaScript, which means that all JavaScript code is valid TypeScript code. TypeScript adds a lot of new features on top of that.
2+
3+
TypeScript makes JavaScript more like a strongly-typed, object-oriented language akin to C# and Java. This means that TypeScript code tends to be easier to use for large projects and that code tends to be easier to understand and maintain. The strong typing also means that the language can (and is) precompiled and that variables cannot be assigned values that are out of their declared range. For instance, when a TypeScript variable is declared as a number, you cannot assign a text value to it.
4+
5+
This strong typing and object orientation makes TypeScript easier to debug and maintain, and those were two of the weakest points of standard JavaScript.
6+
7+
You can add type declarations to variables, function parameters and function return types. The type is written after a colon following the variable name, like this: var num: number = 5; The compiler will then check the types (where possible) during compilation and report type errors.
8+
```
9+
var num: number = 5;
10+
num = "this is a string";
11+
// error: Type 'string' is not assignable to type 'number'.
12+
```
13+
14+
- The basic types are :
15+
- number (both integers and floating point numbers)
16+
- string
17+
- boolean
18+
- Array . You can specify the types of an array's elements. There are two equivalent ways to define array types:
19+
- Array<T> and T[] . For example:
20+
- number[] - array of numbers
21+
- Array<string> - array of strings
22+
- Tuples. Tuples have a fixed number of elements with specific types.
23+
- [boolean, string] - tuple where the first element is a boolean and the second is a string.
24+
- [number, number, number] - tuple of three numbers.
25+
- {} - object, you can define its properties or indexer
26+
- {name: string, age: number} - object with name and age attributes
27+
- {[key: string]: number} - a dictionary of numbers indexed by string
28+
- enum - { Red = 0, Blue, Green } - enumeration mapped to numbers
29+
- Function. You specify types for the parameters and return value:
30+
- (param: number) => string - function taking one number parameter returning string
31+
- () => number - function with no parameters returning an number.
32+
- (a: string, b?: boolean) => void - function taking a string and optionally a boolean with no return value.
33+
- any - Permits any type. Expressions involving any are not type checked.
34+
- void - represents "nothing", can be used as a function return value. Only null and undefined are part of the void type.
35+
- never
36+
- let foo: never; -As the type of variables under type guards that are never true.
37+
- function error(message: string): never { throw new Error(message); } - As the return type of functions that never return.
38+
- null - type for the value null . null is implicitly part of every type, unless strict null checks are enabled.

chapters/1/codes/1.3/app.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class myContainer {
2+
public myStr: string;
3+
constructor(message: string) {
4+
this.myStr = message;
5+
}
6+
getData(): string {
7+
return this.myStr;
8+
}
9+
};
10+
11+
let createObj = new myContainer("Hi Morol!");
12+
console.log(createObj);
13+
console.log(createObj.getData());

chapters/1/codes/1.4/app.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ts-node is an npm package which allows the user to run typescript files directly, without the need for precompilation using tsc . It also provides REPL.
2+
3+
### Install ts-node globally using
4+
- `npm install -g ts-node`
5+
6+
### ts-node does not bundle typescript compiler, so you might need to install it.
7+
- `npm install -g typescript`
8+
9+
### Executing script
10+
To execute a script named main.ts, run
11+
- `ts-node main.ts`
12+
13+
### main.ts (example file)
14+
console.log("Hello world");
15+
16+
### Example usage
17+
- $ `ts-node main.ts`
18+
Hello world
19+
20+
### Running REPL
21+
To run REPL run command ts-node
22+
23+
- Example usage
24+
```
25+
$ `ts-node`
26+
> const sum = (a, b): number => a + b;
27+
> `undefined`
28+
> `sum(2, 2)`
29+
> 4
30+
> .exit
31+
```
32+
33+
To exit REPL use command `.exit` or press `CTRL+C` twice.

chapters/1/codes/1.5/app.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### For use TypeScript REPL in Node.js you can use tsun package
2+
- Install it globally with
3+
- `npm install -g tsun`
4+
5+
and run in your terminal or command prompt with tsun command
6+
7+
### Usage example:
8+
```
9+
$ tsun
10+
TSUN : TypeScript Upgraded Node
11+
12+
type in TypeScript expression to evaluate
13+
type :help for commands in repl
14+
15+
$ function multiply(x, y) {
16+
return x * y;
17+
}
18+
undefined
19+
20+
$ multiply(3, 4)
21+
12
22+
```

chapters/1/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Getting started with Node.js
2+
3+
| **Topic** | **Code** |
4+
|:---------:|:--------:|
5+
| Section 1.1: Installation and setup | [code-here](/chapters/1/codes/1.1/app.md) |
6+
| Section 1.2: Basic syntax | [code-here](/chapters/1/codes/1.2/app.md) |
7+
| Section 1.3: Hello World | [code-here](/chapters/1/codes/1.3/app.ts) |
8+
| Section 1.4: Running TypeScript using ts-node | [code-here](/chapters/1/codes/1.4/app.md) |
9+
| Section 1.5: TypeScript REPL in Node.js | [code-here](/chapters/1/codes/1.5/app.md) |

chapters/2/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# npm
2+
3+
| **Topic** | **Code** |
4+
|:---------:|:--------:|
5+
| Section 2.1: Safety | [code-here](/chapters/2/codes/2.0/app.md) |
6+
| Section 2.2: Readability | [code-here](/chapters/2/codes/2.1/app.md) |
7+
| Section 2.3: Tooling | [code-here](/chapters/2/codes/2.2/app.md) |

0 commit comments

Comments
 (0)