Skip to content

Commit

Permalink
chore(deps): add project deps instead of per chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonSooter committed Mar 15, 2024
1 parent f17f482 commit eaeacb1
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 314 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.cjs
@@ -0,0 +1,7 @@
/* eslint-env node */
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true
};
7 changes: 0 additions & 7 deletions chapter-2/.eslintrc.cjs

This file was deleted.

1 change: 0 additions & 1 deletion chapter-2/.gitignore

This file was deleted.

36 changes: 18 additions & 18 deletions chapter-2/chapter-2-notes.md
Expand Up @@ -11,13 +11,13 @@

## Definitions

- **Type System**: a set of rules that a typechecker uses to assign types to
your program
- **Type System**: a set of rules that a typechecker uses to assign types to
your program

## Two kinds of Type Systems

- Explicit Syntax: it is required to tell the compiler what type everything is
- Inference: Allow the compiler to infer the type of everything automatically
- Explicit Syntax: it is required to tell the compiler what type everything is
- Inference: Allow the compiler to infer the type of everything automatically

TypeScript is inspired by both.

Expand All @@ -44,22 +44,22 @@ let c = [true, false]; // c is an array of booleans

```json
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"lib": ["es"],
"outDir": "dist",
"sourceMap": true,
"removeComments": false
},
"include": ["src/**/*"]
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"lib": ["es"],
"outDir": "dist",
"sourceMap": true,
"removeComments": false
},
"include": ["src/**/*"]
}
```

## ESLint w/ TypeScript (instead of TSLint)

- [https://typescript-eslint.io/getting-started/](https://typescript-eslint.io/getting-started/)
- [https://typescript-eslint.io/getting-started/](https://typescript-eslint.io/getting-started/)
8 changes: 0 additions & 8 deletions chapter-2/dist/index.js

This file was deleted.

File renamed without changes.
12 changes: 0 additions & 12 deletions chapter-2/tsconfig.json

This file was deleted.

33 changes: 17 additions & 16 deletions chapter-3/src/exercises.ts → chapter-3/exercises.ts
@@ -1,51 +1,52 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
export default null; // Force module mode

/**
* 1. For each of these values, what type will TypeScript infer?
* Question 1: For each of these values, what type will TypeScript infer?
*/
let a = 1042; // number
let b = 'apples and oranges'; // string
const a = 1042; // number
const b = 'apples and oranges'; // string
const c = 'pineapples'; // 'pineapples' (type literal)
let d = [true, true, false]; // boolean[]
let e = { type: 'ficus' }; // { type: string }
let f = [1, false]; // (number | boolean)[]
const d = [true, true, false]; // boolean[]
const e = { type: 'ficus' }; // { type: string }
const f = [1, false]; // (number | boolean)[]
const g = [3]; // number[]
let h = null; // any (type widening)
const h = null; // any (type widening)

/**
* 2. Why does each of these throw the error it does?
* Question 2: Why does each of these throw the error it does?
*/

// 2a
let i: 3 = 3;
i = 4; // Error TS2322: Type '4' is not assignable to type '3'.
// Question 2a:
const i: 3 = 3;
// i = 4; // Error TS2322: Type '4' is not assignable to type '3'.

/**
* Because `i` is a type literal, it can only be assigned the value 3.
* The type of 3 is the type, thus 4 is not assignable to 3
*/

// 2b
let j = [1, 2, 3];
const j = [1, 2, 3];
j.push(4);
j.push('5'); // Error TS2345: Argument of type '"5"' is not assignable to parameter of type 'number'.
// j.push('5'); // Error TS2345: Argument of type '"5"' is not assignable to parameter of type 'number'.

/**
* TypeScript infers the type of `j` to be number[] as that is now it was initialized
* The type of '5' is the type literal '5', which is not assignable to number
*/

// 2c
let k: never = 4; // Error TS2322: Type '4' is not assignable to type 'never'.
// let k: never = 4; // Error TS2322: Type '4' is not assignable to type 'never'.

/**
* The type of 4 is number, which is not assignable to `never`
* `never` is assignaable to every other type, but no type is assignable to never
*/

// 2d
let l: unknown = 4;
let m = l * 2; // Error TS2571: Object is of type 'unknown'.
const l: unknown = 4;
// let m = l * 2; // Error TS2571: Object is of type 'unknown'.

/**
* The type of `l` is unknown, which is not a number
Expand Down
File renamed without changes.
73 changes: 0 additions & 73 deletions chapter-5/chessEngine.ts
@@ -1,73 +0,0 @@
type Color = 'Black' | 'White';

/**
* Represent a Chess game
*/
class Game {
private pieces = Game.makePieces();

private static makePieces() {
return [
// Kings
new King('White', 'E', 1),
new King('Black', 'F', 8),

// Queens
// new Queen('White', 'D', 1),
// new Queen('Black', 'D', 8),

// Bishops
// new Bishop('White', 'C', 1),
// new Bishop('White', 'F', 1),
// new Bishop('Black', 'C', 8),
// new Bishop('Black', 'F', 8),

// ...
];
}
}

/**
* A set of coordinates for a piece
*/
type ChessFile = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H';
type ChessRank = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;

class Position {
constructor(private file: ChessFile, private rank: ChessRank) {}
distanceFrom(position: Position) {
return {
rank: Math.abs(position.rank - this.rank),
file: Math.abs(position.file.charCodeAt(0) - this.file.charCodeAt(0)),
};
}
}

/**
* Represent a Chess piece
*/
abstract class Piece {
protected position: Position;
constructor(private readonly color: Color, file: ChessFile, rank: ChessRank) {
this.position = new Position(file, rank);
}
moveTo(position: Position) {
this.position = position;
}
abstract canMoveTo(position: Position): boolean;
}

/**
* The six types of pieces
*/
class King extends Piece {
canMoveTo(position: Position) {
let distance = this.position.distanceFrom(position);
return distance.rank < 2 && distance.file < 2;
}
}
// class Queen extends Piece {}
// class Bishop extends Piece {}
// class Knight extends Piece {}
// class Rook extends Piece {}
// class Pawn extends Piece {}
82 changes: 82 additions & 0 deletions chapter-5/scratchpad.ts
@@ -1 +1,83 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Implementation of a Chess Engine
*/
type Color = 'Black' | 'White';

/**
* Represent a Chess game
*/
class Game {
private pieces = Game.makePieces();

private static makePieces() {
return [
// Kings
new King('White', 'E', 1),
new King('Black', 'F', 8)

// Queens
// new Queen('White', 'D', 1),
// new Queen('Black', 'D', 8),

// Bishops
// new Bishop('White', 'C', 1),
// new Bishop('White', 'F', 1),
// new Bishop('Black', 'C', 8),
// new Bishop('Black', 'F', 8),

// ...
];
}
}

/**
* A set of coordinates for a piece
*/
type ChessFile = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H';
type ChessRank = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;

class Position {
constructor(private file: ChessFile, private rank: ChessRank) {}
distanceFrom(position: Position) {
return {
rank: Math.abs(position.rank - this.rank),
file: Math.abs(
position.file.charCodeAt(0) - this.file.charCodeAt(0)
)
};
}
}

/**
* Represent a Chess piece
*/
abstract class Piece {
protected position: Position;
constructor(
private readonly color: Color,
file: ChessFile,
rank: ChessRank
) {
this.position = new Position(file, rank);
}
moveTo(position: Position) {
this.position = position;
}
abstract canMoveTo(position: Position): boolean;
}

/**
* The six types of pieces
*/
class King extends Piece {
canMoveTo(position: Position) {
const distance = this.position.distanceFrom(position);
return distance.rank < 2 && distance.file < 2;
}
}
// class Queen extends Piece {}
// class Bishop extends Piece {}
// class Knight extends Piece {}
// class Rook extends Piece {}
// class Pawn extends Piece {}

0 comments on commit eaeacb1

Please sign in to comment.