Share
Embed
Transcript
90 slides
-
Ginpei.dev By Ginpei Takanashi https://Ginpei.dev Start TypeScript . Leaving JavaScript
as JS JSConf JP December 1st 2019 -
Ginpei.dev • This slide is designed for presentation. PDF would
not make sence. • I’m preparing an article for it. Stay tuend! • このスライドはプレゼン用です。PDFだけだとわかりづらいと 思います。 • 記事用意してるのでお待ちくださいませ。 ⚠️ Caution ご注意 ⚠️ -
Ginpei.dev Target Individual Job AIに支配される人達のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2016/08/ai.html オフィスのイラスト
| かわいいフリー素材集 いらすとや https://www.irasutoya.com/2014/05/blog-post_6681.html 不良集団のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2019/07/blog-post_37.html 通帳を見て青ざめる人のイラスト(男性) | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2017/08/blog-post_880.html https://github.com/remojansen/logo.ts Target -
Ginpei.dev /** * @param {Size} size */ function setSize(size) {}
/** @type {string | null} */ let id = null; /** * @typedef {{ * height: number; * width: number; * }} Size */ Agenda • Add comments to declare types in JS files • TYPE IS AWESOME o Improves your coding speed o Supports your refactoring • Use `tsc` as a type linter o With `--checkJs` option • Less cost than converting JS to TS • Individual usage possible -
Ginpei.dev I’m Ginpei / ギンペイです From Vancouver, Canada Love JavaScript
& JapaneseSushi 世界地図のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2013/02/blog-post_8574.html -
Ginpei.dev • TypeScript is superset of JavaScript • TypeScript has
types How TypeScript is AWESOME -
Ginpei.dev function greeter(person) { return "Hello, " + person; }
let user = "Jane User"; document.body.textContent = greeter(user); JavaScript is-a TypeScript Via TypeScript in 5 minutes · TypeScript https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html -
Ginpei.dev function greeter(person: string) { return "Hello, " + person;
} let user = "Jane User"; document.body.textContent = greeter(user); Types Via TypeScript in 5 minutes · TypeScript https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html -
Ginpei.dev In some cases, its tough to add TS step
into the current bundler Traditional Long-life Build System tsc 本能寺のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2017/01/blog-post_0.html 鹿に鹿せんべいを上げる人のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2018/10/blog-post_31.html -
Ginpei.dev In some cases, its tough to add TS step
into the current bundler 本能寺の変のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2017/01/blog-post_16.html 鹿に襲われる人のイラスト | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2018/10/blog-post_38.html -
Ginpei.dev On the other hand, team may feel uncomfortable with
TypeScript いらすとや https://www.irasutoya.com/2014/10/blog-post_89.html https://www.irasutoya.com/2014/10/blog-post_35.html https://www.irasutoya.com/2017/08/blog-post_915.html https://www.irasutoya.com/2018/06/crt.html https://www.irasutoya.com/2014/04/blog-post_7628.html remojansen/logo.ts https://github.com/remojansen/logo.ts -
Ginpei.dev On the other hand, team may feel uncomfortable with
TypeScript いらすとや https://www.irasutoya.com/2014/10/blog-post_89.html https://www.irasutoya.com/2014/10/blog-post_35.html https://www.irasutoya.com/2017/08/blog-post_915.html https://www.irasutoya.com/2018/06/crt.html https://www.irasutoya.com/2014/04/blog-post_7628.html remojansen/logo.ts https://github.com/remojansen/logo.ts -
Ginpei.dev Even if there are no build steps FFFTP Project
Top Page - OSDN https://osdn.net/projects/ffftp/ 田舎の村のイラスト(過疎) | かわいいフリー素材集 いらすとや https://www.irasutoya.com/2017/06/blog-post_392.html -
Ginpei.dev OK no worries, Typed-JavaScript is here!!! /** * @param
{Size} size */ function setSize(size) {} /** @type {string | null} */ let id = null; /** * @typedef {{ * height: number; * width: number; * }} Size */ -
Ginpei.dev • Typed-JavaScript is definitely JavaScript • Typed-JavaScript has types
Typed-JavaScript is AWESOME -
Ginpei.dev TypeScript vs Typed-JavaScript function setSize(size: Size) {} let id:
string | null = null; let Size = { height: number; width: number; } /** * @param {Size} size */ function setSize(size) {} /** @type {string | null} */ let id = null; /** * @typedef {{ * height: number; * width: number; * }} Size */ -
Ginpei.dev You could start typing alone $ npm install typescript
--global $ npx tsc -
Ginpei.dev /** * @param {Size} size */ function setSize(size) {}
/** @type {string | null} */ let id = null; /** * @typedef {{ * height: number; * width: number; * }} Size */ Typed-JavaScript is definitely JavaScript -
Ginpei.dev setSize({ height: { value: 123, unit: 'px' }, width:
{ value: 234, unit: 'px' }, }); Guess parameter: complex object? -
Ginpei.dev const book = { auther: 'Ginpei Takanashi', name: 'JavaScript
for Adélie Penguins', publishedAt: new Date('2019/12/01'), }; Will you be satisfied by this code? -
Ginpei.dev const book = { auther: 'Ginpei Takanashi', name: 'JavaScript
for Adélie Penguins', publishedAt: new Date('2019/12/01'), }; Do you want to correct typo? -
Ginpei.dev const book = { auther: 'Ginpei Takanashi', name: 'JavaScript
for Adélie Penguins', publishedAt: new Date('2019/12/01'), }; Do you want to rename better? -
Ginpei.dev const book = { auther: 'Ginpei Takanashi', name: 'JavaScript
for Adélie Penguins', publishedAt: new Date('2019/12/01'), }; Do you want to alternate type? -
Ginpei.dev $ npm install typescript $ npx tsc --init message
TS6071: Successfully created a tsconfig.json file. Install and set up -
Ginpei.dev { "compilerOptions": { /* Basic Options */ // "incremental":
true, /* Enable incremental compilation */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'E "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 's // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react- // "declaration": true, /* Generates corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ … Generated `tsconfig.json` file -
Ginpei.dev { "compilerOptions": { "allowJs": true, // read *.js "checkJs":
true, // check types in JS "noEmit": true, // only type-checking "strict": true, // enable nice type-checking options }, "include": ["./js"], // target files "exclude": ["./js/lib"] // not-target files } Example `tsconfig.json` -
Ginpei.dev // @ts-nocheck import { Book } from "./Book"; export
default function MyComponent(props) { return ( <div> <h1>Hello World!</h1> </div> ); } Skip error report for a file -
Ginpei.dev // @ts-nocheck import { Book } from "./Book"; export
default function MyComponent(props) { return ( <div> <h1>Hello World!</h1> </div> ); } Skip error report for a file -
Ginpei.dev { "compilerOptions": { "allowJs": true, // read *.js -
"checkJs": true, // check types in JS + // "checkJs": true, // check types in JS "noEmit": true, // only type-checking "strict": true, // enable nice type-checking options }, } Check types in specific files -
Ginpei.dev // @ts-check import { Book } from "./Book"; export
default function MyComponent(props) { return ( <div> <h1>Hello World!</h1> </div> ); } Report error for each file -
Ginpei.dev // @ts-check import { Book } from "./Book"; export
default function MyComponent(props) { return ( <div> <h1>Hello World!</h1> </div> ); } Report error for each file -
Ginpei.dev { "compilerOptions": { "allowJs": true, // read *.js "checkJs":
true, // check types in JS - "noEmit": true, // only type-checking + "outDir": "./out", "strict": true, // enable nice type-checking options }, } Use TypeScript together * In default src/index.ts ↓ src/index.js -
Ginpei.dev // TypeScript function setSize(size: Size): boolean {} Functions //
JavaScript /** * @param {Size} size * @returns {boolean} */ function setSize(size) {} -
Ginpei.dev // TypeScript function isSize(size: any): size is Size {}
Functions (type guard) // JavaScript /** * @param {any} size * @returns {size is Size} */ function isSize(size) {} -
Ginpei.dev // TypeScript let id: string | null = null;
Variables // JavaScript /** @type {string | null} */ let id = null; -
Ginpei.dev // TypeScript const elInput = document.querySelector('#input') as HTMLInputElement; Expressions
with cast // JavaScript /** @type {HTMLInputElement} */ const elInput = (document.querySelector('#input')); -
Ginpei.dev // TypeScript const map = new Map<string, string>(); Variable
with Generics // JavaScript /** @type {Map<string, string>} */ const map = new Map(); -
Ginpei.dev // TypeScript type ItemId = string | null; Types
(simple) // JavaScript /** * @typedef {string | null} ItemId */ -
Ginpei.dev // TypeScript type Size = { height: number; width:
number; } Types (complex) // JavaScript /** * @typedef {{ * height: number; * width: number; * }} Size */ -
Ginpei.dev // TypeScript type Size = import('./lib').Size; Import types //
JavaScript /** * @typedef {import('./path/to').Size} Size */ -
Ginpei.dev // TypeScript function wrap<T>(item: T | T[]) { return
item instanceof Array ? item : [item]; } Generics (template) -
Ginpei.dev // JavaScript /** * @template T * @param {T
| T[]} item * @returns {T[]} */ function wrap (item) { return item instanceof Array ? item : [item]; } Generics (template) -
Ginpei.dev // TypeScript function findById<T extends { id: string }>(arr:
T[], id: string) { return arr.find((v) => v.id === id); } Generics (template) (extends) -
Ginpei.dev // JavaScript /** * @template {{ id: string }}
T * @param {T[]} arr * @param {string} id */ function findById (arr, id) { return arr.find((v) => v.id === id); } Generics (template) (extends) -
Ginpei.dev • `npm install @types/jquery` • Create own `./types/jquery.d.ts` Add
missing declarations -
Ginpei.dev Recap: Add types into your code • Functions o`@param`
o`@returns` • Variables o`@type` o`(exp)` to cast • Types o`@typedef` o`import()` • Generics o`@template` • Skip error report o `// @ts-ignore` -
Ginpei.dev // TypeScript const book = { author: 'Ginpei Takanashi',
title: 'JavaScript for Adélie Penguins', publishedAt: new Date('2019/12/01'), }; type Book = typeof book; `typeof` type Book = { author: string; title: string; publishedAt: Date; } -
Ginpei.dev // TypeScript const book = { author: 'Ginpei Takanashi',
title: 'JavaScript for Adélie Penguins', publishedAt: new Date('2019/12/01'), }; /** * @typedef {typeof book} Book */ `typeof` -
Ginpei.dev function buildLargeObject() { return { foo: { hoge: 123
}, bar: [true, false, true], run() { return true; }, /** * @param {string} destination */ walk(destination) {}, }; } /** * @typedef {ReturnType<buildLargeObject>} LargeObject */ Util: `ReturnType<fn>` type LargeObject = { foo: { hoge: number; }; bar: boolean[]; run(): boolean; walk(destination: string): void; } -
Ginpei.dev /** * @param {FooOptions} options */ function foo(options) {}
/** @type {Parameters<foo>[0]} */ const options = {}; foo(options); Util: `Paramerters<fn>[i]` -
Ginpei.dev /** * Obtain the parameters of a constructor function
type in a tuple */ type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never; Util: `ConstructorParameters<fn>` -
Ginpei.dev type AnyObject<T = {}> = T & { [key:
string]: any; } const book: AnyObject<Partial<Book>> = {}; book.author = getAuthor(); Any object -
Ginpei.dev type PickAngularService<T extends any[]> = ReturnType<Exclude<T[0], string>>; namespace App
{ type MyService = PickAngularService<typeof import('path/to/lib')> } e.g. AngularJS Service Type -
Ginpei.dev Much code function setSize(size: Size) {} let id: string
| null = null; let Size = { height: number; width: number; } /** * @param {Size} size */ function setSize(size) {} /** @type {string | null} */ let id = null; /** * @typedef {{ * height: number; * width: number; * }} Size */ -
Ginpei.dev // TypeScript const elInput = document.querySelector<HTMLInputEle ment>('#input'); const elementMap
= new Map<string, HTMLElement>(); No runtime generics -
Ginpei.dev // TypeScript if (map.has('xxx')) { return map.get('xxx')!; } No
non-null assertion `!` operators -
Ginpei.dev No default types in generics // JavaScript // //
TypeScript function foo<T = {}>() {} -
Ginpei.dev /** * @param {Size} size */ function setSize(size) {}
/** @type {string | null} */ let id = null; /** * @typedef {{ * height: number; * width: number; * }} Size */ Recap • Add comments to declare types in JS files • TYPE IS AWESOME o Improves your coding speed o Supports your refactoring • Use `tsc` as a type linter o With `--checkJs` option • Easy to get started • Enjoy typing!