diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/01_simple/index.ts b/01_simple/index.js similarity index 100% rename from 01_simple/index.ts rename to 01_simple/index.js diff --git a/03_more_options/Readme.md b/03_more_options/Readme.md new file mode 100644 index 0000000..085d8a9 --- /dev/null +++ b/03_more_options/Readme.md @@ -0,0 +1,35 @@ +# Другие настройки + +Откройте файл **tsconfig.json** + +## Настройка strict + +- Запустите команду **tsc** + +> Убедитесь что ошибок типизации нет + +- Выставьте strict = true + +- Повторите команду **tsc** + +> В терминале должны появиться ошибки + +![call error](../assets/03.png) + +Подробнее [почитайте](https://www.typescriptlang.org/tsconfig/#strict) об этой настройке + +2. Настройки **sourceMap** и **declaration** + +```json +{ + "compilerOptions": { + "declaration": true, + "sourceMap": true + } +} +``` + +* Обратите внимание какие файлы появляются в **dist** кроме **.js** + +> Посмотрите что именно в них написано и откуда эта информация появляется + diff --git a/03_more_options/dist/index.js b/03_more_options/dist/index.js index 1ffed85..9d29eb0 100644 --- a/03_more_options/dist/index.js +++ b/03_more_options/dist/index.js @@ -1,6 +1,7 @@ import { add } from './utils'; const a = 1; -const b = 2; +const b = "2"; const c = 3; -console.info(add(a, b)); +console.info(add(a, parseInt(b))); +console.log(add.call(undefined, a, b)); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/03_more_options/dist/index.js.map b/03_more_options/dist/index.js.map index 36b297c..6d2a45b 100644 --- a/03_more_options/dist/index.js.map +++ b/03_more_options/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAE9B,MAAM,CAAC,GAAW,CAAC,CAAC;AACpB,MAAM,CAAC,GAAW,CAAC,CAAC;AACpB,MAAM,CAAC,GAAG,CAAC,CAAC;AAEZ,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAE9B,MAAM,CAAC,GAAG,CAAC,CAAC;AACZ,MAAM,CAAC,GAAG,GAAG,CAAC;AACd,MAAM,CAAC,GAAG,CAAC,CAAC;AAEZ,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAElC,OAAO,CAAC,GAAG,CACP,GAAG,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC,CAC5B,CAAA"} \ No newline at end of file diff --git a/03_more_options/src/index.ts b/03_more_options/src/index.ts index 6d1ef2e..dd0a249 100644 --- a/03_more_options/src/index.ts +++ b/03_more_options/src/index.ts @@ -1,7 +1,11 @@ import { add } from './utils'; -const a: number = 1; -const b: number = 2; +const a = 1; +const b = "2"; const c = 3; -console.info(add(a, b)) +console.info(add(a, parseInt(b))); + +console.log( + add.call(undefined, a, b) +) diff --git a/03_more_options/test.js b/03_more_options/test.js index 74b6421..e058f1e 100644 --- a/03_more_options/test.js +++ b/03_more_options/test.js @@ -1,4 +1,4 @@ -// @ts-check +// @ts-check-off import { add } from './dist/utils' add(1, '2') diff --git a/04_lint/Readme.md b/04_lint/Readme.md new file mode 100644 index 0000000..6f38db4 --- /dev/null +++ b/04_lint/Readme.md @@ -0,0 +1,28 @@ +# Lint + +Установите зависимости + +```shell +npm ci +``` + +Запуустите команду проверки + +```shell +npm run eslint +``` + +> Обратите внимание на ошибки в терминале + +* Откройте файлы с ошибками и устраните их + +> Обратите внимание что настройки eslint для javascript не всегда корректно работают с typeescript + +![js eslint ignore problem](../assets/04.png) + +> Поменяйте директиву на подходящую для **typeescript** +``` +eslint-disable-next-line @typescript-eslint/no-unused-vars +``` + +> Ошибка ушла diff --git a/04_lint/example.ts b/04_lint/example.ts index 49b1c10..6dd586e 100644 --- a/04_lint/example.ts +++ b/04_lint/example.ts @@ -5,4 +5,5 @@ const summ = (a, b) => { return a + b; } +// eslint-disable-next-line no-unused-vars const boysAndGirls = summ(boys, girls); diff --git a/04_lint/package.json b/04_lint/package.json index c7735ae..406cdaf 100644 --- a/04_lint/package.json +++ b/04_lint/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "lint": "eslint ." }, "keywords": [], "author": "", diff --git a/05_error/Readme.md b/05_error/Readme.md new file mode 100644 index 0000000..dfa5468 --- /dev/null +++ b/05_error/Readme.md @@ -0,0 +1,25 @@ +# Ошибки + +> Откройте [пример](./animals.ts) + +* запустите typesscript + +```shell +tsc +``` + +> Запустите полученный javascript файл + +```shell +node ./animal.js +``` + +> Обратите внимание на ошибку при запуске + +![ts error](../assets/05.png) + +> Определите почему именно возникла ошибка + +> Обратите внимание что typescript не указал на ошибку до runtime + +[Полезные ссылки](./links.md) diff --git a/05_error/animals.js b/05_error/animals.js deleted file mode 100644 index b9a93e3..0000000 --- a/05_error/animals.js +++ /dev/null @@ -1,40 +0,0 @@ -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - if (typeof b !== "function" && b !== null) - throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -var Animal = /** @class */ (function () { - function Animal() { - } - return Animal; -}()); -var Dog = /** @class */ (function (_super) { - __extends(Dog, _super); - function Dog() { - return _super !== null && _super.apply(this, arguments) || this; - } - Dog.prototype.bark = function () { }; - return Dog; -}(Animal)); -var Cat = /** @class */ (function (_super) { - __extends(Cat, _super); - function Cat() { - return _super !== null && _super.apply(this, arguments) || this; - } - Cat.prototype.meow = function () { }; - return Cat; -}(Animal)); -var cage1 = [new Dog(), new Dog()]; -var cage2 = cage1; // ok -cage2.push(new Cat()); // ok -cage1.forEach(function (dog) { return dog.bark(); }); // ok diff --git a/06_complex/Readme.md b/06_complex/Readme.md new file mode 100644 index 0000000..b28cee4 --- /dev/null +++ b/06_complex/Readme.md @@ -0,0 +1,3 @@ +# Разберите приведённые примеры + +> обратите внимание как применяется infer \ No newline at end of file diff --git a/06_complex/infer.ts b/06_complex/infer.ts index 9783342..caba1b5 100644 --- a/06_complex/infer.ts +++ b/06_complex/infer.ts @@ -14,4 +14,4 @@ const double = (x: number, s: 'foo') => x * 2; // get type of first argument of function type GetFirstFuncArg = Arg extends (a, x: infer T) => any ? T : never; -type DoubleArgType = GetFirstFuncArg \ No newline at end of file +type DoubleArgType = GetFirstFuncArg diff --git a/assets/03.png b/assets/03.png new file mode 100644 index 0000000..e14b698 Binary files /dev/null and b/assets/03.png differ diff --git a/assets/04.png b/assets/04.png new file mode 100644 index 0000000..c753c2f Binary files /dev/null and b/assets/04.png differ diff --git a/assets/05.png b/assets/05.png new file mode 100644 index 0000000..09f032a Binary files /dev/null and b/assets/05.png differ