One Does Not… write TypeScript so easily! In this Meetup talk, I'll share the tricks and pain points I had to learn in my first 6 months of professional TypeScript. The goal is to spare the reader many hours of Stack Overflow...
application developer, I don't like any scripting or dynamic languages. I like code that compiles (...) TypeScript looks like it will help with some of the problems like having (...) compiling (…) » (somewhere on the net) No. You are asking for a linter. All languages have linters. JS already has an excellent linter: ESLlint TypeScript also has a linter: TSLint They will catch typos and code smells. Use a linter. Now.
Embraced by Google: Angular 2 • Big contributions from Palantir (tslint, blueprint, …) • Webpack is considering a rewrite in typescript Following strong players’ lead may be a good move, But could you make your own opinion ?
documentation – Collaboration – Faster usage • Types = catching (some) errors – Interfaces – early • Refactoring • ... • New language to learn • NOT trivial • More complicated stack • Typings (find, fix, update…) • Source maps • Can’t use some JS tooling • Bugs • Lagging behind the standard • Static typing sometimes refuses good programs and has to be painfully hacked • ...
documentation – Collaboration – Faster usage • Types = catching (some) errors – Interfaces – early • Refactoring • ... • New language to learn • NOT trivial • More complicated stack • Typings (find, fix, update…) • Source maps • Can’t use some JS tooling • Bugs • Lagging behind the standard • Static typing sometimes refuses good programs and has to be painfully hacked • ... Short-term Long-term & important
(3+) • Do you have time right now ? • Is it a long-term project or just a MVP ? • Are you experienced ? • Does your industry need the best ? (ex. Security) • Do you like Typescript ?
Not a substitute for Code Reviews • Not a way to go back to your OOP comfort zone – Please learn about functional programming A Gentle Introduction to Functional JavaScript
declared (more about this later) { "compileOnSave": false, "compilerOptions": { "allowJs": false, "noEmitOnError": true, "noImplicitAny": true, (...) "strictNullChecks": true, "target": "ES6", "module": "ES6", "declaration": true, "sourceMap": true, "outDir": “temp/src.es6" }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] } new project from scratch or from existing code ? choose the errors to catch ! or else your IDE may launch transpilation ! output control
{[k: string]: Color } • JSON is a declared type, can be extended: interface JsonSchema extends JSON { title: string additionalProperties: boolean [k: string]: any }
compatible type: let x: any = { name: ‘Lothar’, age: 32 } let user: Person = x as Person • Force casting let x: any = { name: ‘Lothar’ } let user: Person = x as any as Person Don’t overuse it ! Valid use case: mock data in tests
special kind of TypeScript file which “annotate” some JS: npm I -D @types/lodash • You NEED them. But may not exist, not be correct, not be up-to-date • @types picked automatically by the compiler (since typescript 2) • Sometimes needed: import * as _ from ‘lodash’ • Write your own: official doc, tutorial (More about this in next talk !) http://stackoverflow.com/questions/38224232/how-to-consume-npm-modules-from-typescript
stable, ES6, UMD • Generate and expose the typings • NEVER use default exports: babel and typescript won’t work well (see next slide) • Beware of conflicting environments – Node vs. browser on SetTimeout
well with all platforms • Use only named exports export { Foo, bar } • Named exports are recommended in TypeScript regardless of this issue • Interesting reads: – http://stackoverflow.com/a/38671949/587407 – http://blog.jonasbandi.net/2016/10/myth-of-superset.html#comment-29295611 55 – http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/ – https://github.com/webpack/webpack/issues/706
guidelines • TypeScript formatter: tsfmt • Call the typescript compiler from a script: – node-typescript-compiler – Allows to dynamically change config • Useful npm script: syntax watch: "tsc:watch": "npm run tsc -- --watch", • In special cases, you may have to tweak the –lib option: --lib webworker
Comparison : C++ superset of C ? • Superset of WELL-WRITTEN JavaScript ? • http://blog.jonasbandi.net/2016/10/myth-of-superset.html#co mment-2929561155 https://smellegantcode.wordpress.com/2015/07/11/is-typescript-really-a-superset-of-javascript-and-does-it-even-matter/
https://github.com/Offirmo-team/wiki/wiki/typescript • I was wrong about typescript https://news.ycombinator.com/item?id=11841502 • http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/ • http://www.jbrantly.com/typescript-and-webpack/ •