├── README.md
├── errors.js
├── logo.png
├── no-any.js
├── nominal-types.js
├── operators.js
├── parametric-polymorphism.js
├── related-types.js
└── type-refinement.js
/README.md:
--------------------------------------------------------------------------------
1 | # Hegel Literature and Demos
2 |
3 |
4 |
5 |
6 | | Тема | Ссылки |
7 | | ----------------------------- | ----------------------------------------------------------------- |
8 | | There is a bluebird in my talk that wants to get out | https://www.youtube.com/watch?v=NCf5nthB1tA |
9 | | AST в JavaScript | https://www.youtube.com/watch?v=ILSpvViUlPU |
10 | | Системы типов в двух словах | https://www.youtube.com/watch?v=nFtO6419A5k |
11 | | Б.Пирс Типы в ЯП | https://www.ozon.ru/context/detail/id/7410082 |
12 | | TDD with Idris | https://www.manning.com/books/type-driven-development-with-idris |
13 | | Структура и Интерпретация Компьютерных Программ | https://www.ozon.ru/context/detail/id/5322055 |
14 | | Dragon Book | https://www.ozon.ru/context/detail/id/3829076 |
15 | | Sea of Nodes | https://darksi.de/d.sea-of-nodes |
16 | | Variances | https://habr.com/ru/post/218753/ |
17 | | Don't use cast | https://gustedt.wordpress.com/2014/04/02/dont-use-casts-i/ |
18 | | Лекции по выводу типов в GHC 8.8 | https://www.youtube.com/watch?v=_HYI7zjkrEs |
19 |
--------------------------------------------------------------------------------
/errors.js:
--------------------------------------------------------------------------------
1 | type User = {
2 | id: number,
3 | email: string
4 | };
5 |
6 | const NOT_FOUND = { status: 404, message: "User not found" };
7 |
8 | function getByEmail(email: string): User {
9 | if (Math.random() > 0.5) {
10 | return { id: 1, email };
11 | }
12 | throw new Error("User is not presented in DB");
13 | }
14 |
15 | function signIn(email: string) {
16 | if (false) {
17 | throw 2;
18 | }
19 | return getByEmail(email);
20 | }
21 |
22 | function signInRoute(email) {
23 | try {
24 | return signIn(email);
25 | } catch (e) {
26 | if (e instanceof Error) {
27 | throw NOT_FOUND;
28 | }
29 | throw e;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JSMonk/hegel-literature/a8a09f335e366d504efdb8655391e120ac5ea4f9/logo.png
--------------------------------------------------------------------------------
/no-any.js:
--------------------------------------------------------------------------------
1 | function toString(obj: any): string {
2 | return `[${obj}]`;
3 | }
--------------------------------------------------------------------------------
/nominal-types.js:
--------------------------------------------------------------------------------
1 | class Animal {}
2 |
3 | class Dog extends Animal {
4 | woof() {}
5 | }
6 |
7 | class Cat extends Animal {
8 | meow() {}
9 | }
10 |
11 | const cats: Array = [new Cat()];
12 | const animals: Array = cats;
13 |
14 | animals[1] = new Dog();
15 |
--------------------------------------------------------------------------------
/operators.js:
--------------------------------------------------------------------------------
1 | type User = { id: number };
2 | const user: ?User= { id: 0 };
3 |
4 | if (user) {}
5 |
6 | for (let i = 10; i--; ) {}
7 |
8 | while(user) {}
9 |
10 | if ("b" in 2) {}
11 |
12 | if (typeof user === "void") {}
--------------------------------------------------------------------------------
/parametric-polymorphism.js:
--------------------------------------------------------------------------------
1 | function id(x) {
2 | return x;
3 | }
4 |
5 | const num = id(2);
6 | const str = id("str");
7 | const fn = id(id);
8 |
--------------------------------------------------------------------------------
/related-types.js:
--------------------------------------------------------------------------------
1 | class User {}
2 | class Chat {}
3 | class Message {}
4 |
5 | const models = { User, Chat, Message };
6 |
7 | type Models = $TypeOf;
8 |
9 | function model>(modelName: T): $PropertyType {
10 | return models[modelName];
11 | }
12 |
13 | // -------------------------------------------------------------------------
14 |
15 | const UserModel = model("User");
16 |
17 | const user: User = new UserModel();
--------------------------------------------------------------------------------
/type-refinement.js:
--------------------------------------------------------------------------------
1 | const variable1: number | Array = 2;
2 | const variable2: { a: { b: number }, c: string } | { a: { b: string }, d: ?boolean } = { a: { b: "str" } };
3 |
4 | if (typeof variable1 === "number") {
5 | const res = variable1;
6 | } else {
7 | const res = variable1;
8 | }
9 |
10 |
11 | if (typeof variable2.a.b === "number") {
12 | const res = variable2.c;
13 | }
14 |
15 | if ("c" in variable2) {
16 | const res = variable2;
17 | } else {
18 | const res = variable2;
19 | }
20 |
21 | if (variable1 instanceof Array) {
22 | const res = variable1;
23 | }
--------------------------------------------------------------------------------