TypeScriptNotesForProfessionals.pdf

(1113 KB) Pobierz
TypeScript
TypeScript
Notes for Professionals
Notes for Professionals
of professional hints and tricks
80+ pages
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial TypeScript group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
................................................................................................................................................................................... 1
Chapter 1: Getting started with TypeScript
.................................................................................................... 2
Section 1.1: Installation and setup
................................................................................................................................. 2
Section 1.2: Basic syntax
............................................................................................................................................... 4
Section 1.3: Hello World
................................................................................................................................................. 5
Section 1.4: Running TypeScript using ts-node
.......................................................................................................... 5
Section 1.5: TypeScript REPL in Node.js
....................................................................................................................... 6
Chapter 2: Importing external libraries
............................................................................................................. 8
Section 2.1: Finding definition files
............................................................................................................................... 8
Section 2.2: Importing a module from npm
............................................................................................................... 9
Section 2.3: Using global external libraries without typings
..................................................................................... 9
Section 2.4: Finding definition files with typescript 2.x
.............................................................................................. 9
Chapter 3: Classes
...................................................................................................................................................... 11
Section 3.1: Abstract Classes
...................................................................................................................................... 11
Section 3.2: Simple class
............................................................................................................................................. 11
Section 3.3: Basic Inheritance
..................................................................................................................................... 12
Section 3.4: Constructors
............................................................................................................................................ 12
Section 3.5: Accessors
................................................................................................................................................. 13
Section 3.6: Transpilation
........................................................................................................................................... 14
Section 3.7: Monkey patch a function into an existing class
.................................................................................. 15
Chapter 4: Strict null checks
................................................................................................................................ 16
Section 4.1: Strict null checks in action
...................................................................................................................... 16
Section 4.2: Non-null assertions
................................................................................................................................. 16
Chapter 5: Using Typescript with React (JS & native)
............................................................................ 18
Section 5.1: ReactJS component written in TypeScript
........................................................................................... 18
Section 5.2: Typescript & react & webpack
.............................................................................................................. 19
Chapter 6: Functions
................................................................................................................................................. 21
Section 6.1: Optional and Default Parameters
......................................................................................................... 21
Section 6.2: Function as a parameter
....................................................................................................................... 21
Section 6.3: Functions with Union Types
................................................................................................................... 23
Section 6.4: Types of Functions
................................................................................................................................. 23
Chapter 7: Interfaces
................................................................................................................................................ 24
Section 7.1: Extending Interface
................................................................................................................................. 24
Section 7.2: Class Interface
........................................................................................................................................ 24
Section 7.3: Using Interfaces for Polymorphism
...................................................................................................... 25
Section 7.4: Generic Interfaces
................................................................................................................................... 26
Section 7.5: Add functions or properties to an existing interface
.......................................................................... 27
Section 7.6: Implicit Implementation And Object Shape
......................................................................................... 27
Section 7.7: Using Interfaces to Enforce Types
........................................................................................................ 28
Chapter 8: Using TypeScript with webpack
.................................................................................................. 29
Section 8.1: webpack.config.js
.................................................................................................................................... 29
Chapter 9: Generics
................................................................................................................................................... 30
Section 9.1: Generic Interfaces
................................................................................................................................... 30
Section 9.2: Generic Class
........................................................................................................................................... 30
Section 9.3: Type parameters as constraints
........................................................................................................... 31
Section 9.4: Generics Constraints
.............................................................................................................................. 31
Section 9.5: Generic Functions
................................................................................................................................... 32
Section 9.6: Using generic Classes and Functions:
.................................................................................................. 32
Chapter 10: TypeScript Core Types
................................................................................................................... 33
Section 10.1: String Literal Types
................................................................................................................................ 33
Section 10.2: Tuple
....................................................................................................................................................... 36
Section 10.3: Boolean
.................................................................................................................................................. 36
Section 10.4: Intersection Types
................................................................................................................................. 37
Section 10.5: Types in function arguments and return value. Number
................................................................. 37
Section 10.6: Types in function arguments and return value. String
..................................................................... 38
Section 10.7: const Enum
............................................................................................................................................ 38
Section 10.8: Number
.................................................................................................................................................. 39
Section 10.9: String
...................................................................................................................................................... 39
Section 10.10: Array
..................................................................................................................................................... 40
Section 10.11: Enum
...................................................................................................................................................... 40
Section 10.12: Any
........................................................................................................................................................ 40
Section 10.13: Void
........................................................................................................................................................ 40
Chapter 11: Integrating with Build Tools
......................................................................................................... 41
Section 11.1: Browserify
................................................................................................................................................ 41
Section 11.2: Webpack
................................................................................................................................................. 41
Section 11.3: Grunt
........................................................................................................................................................ 42
Section 11.4: Gulp
.......................................................................................................................................................... 42
Section 11.5: MSBuild
.................................................................................................................................................... 43
Section 11.6: NuGet
....................................................................................................................................................... 43
Section 11.7: Install and configure webpack + loaders
............................................................................................ 44
Chapter 12: Publish TypeScript definition files
............................................................................................ 45
Section 12.1: Include definition file with library on npm
........................................................................................... 45
Chapter 13: Class Decorator
.................................................................................................................................. 46
Section 13.1: Generating metadata using a class decorator
................................................................................... 46
Section 13.2: Passing arguments to a class decorator
............................................................................................ 46
Section 13.3: Basic class decorator
............................................................................................................................ 47
Chapter 14: tsconfig.json
........................................................................................................................................ 49
Section 14.1: Create TypeScript project with tsconfig.json
...................................................................................... 49
Section 14.2: Configuration for fewer programming errors
................................................................................... 50
Section 14.3: compileOnSave
...................................................................................................................................... 51
Section 14.4: Comments
.............................................................................................................................................. 51
Section 14.5: preserveConstEnums
............................................................................................................................ 52
Chapter 15: Mixins
....................................................................................................................................................... 53
Section 15.1: Example of Mixins
................................................................................................................................... 53
Chapter 16: Enums
...................................................................................................................................................... 54
Section 16.1: Enums with explicit values
..................................................................................................................... 54
Section 16.2: How to get all enum values
.................................................................................................................. 55
Section 16.3: Extending enums without custom enum implementation
................................................................. 55
Section 16.4: Custom enum implementation: extends for enums
.......................................................................... 55
Chapter 17: TypeScript with AngularJS
........................................................................................................... 57
Section 17.1: Directive
.................................................................................................................................................. 57
Section 17.2: Simple example
..................................................................................................................................... 58
Section 17.3: Component
............................................................................................................................................. 58
Chapter 18: TypeScript with SystemJS
............................................................................................................ 60
Section 18.1: Hello World in the browser with SystemJS
......................................................................................... 60
Chapter 19: Unit Testing
.......................................................................................................................................... 63
Section 19.1: tape
.......................................................................................................................................................... 63
Section 19.2: jest (ts-jest)
............................................................................................................................................ 63
Section 19.3: Alsatian
................................................................................................................................................... 66
Section 19.4: chai-immutable plugin
.......................................................................................................................... 66
Chapter 20: TSLint - assuring code quality and consistency
............................................................... 68
Section 20.1: Configuration for fewer programming errors
................................................................................... 68
Section 20.2: Installation and setup
.......................................................................................................................... 68
Section 20.3: Sets of TSLint Rules
.............................................................................................................................. 69
Section 20.4: Basic tslint.json setup
........................................................................................................................... 69
Section 20.5: Using a predefined ruleset as default
................................................................................................ 69
Chapter 21: TypeScript basic examples
.......................................................................................................... 71
Section 21.1: 1 basic class inheritance example using extends and super keyword
............................................. 71
Section 21.2: 2 static class variable example - count how many time method is being invoked
....................... 71
Chapter 22: User-defined Type Guards
.......................................................................................................... 72
Section 22.1: Type guarding functions
...................................................................................................................... 72
Section 22.2: Using instanceof
................................................................................................................................... 73
Section 22.3: Using typeof
.......................................................................................................................................... 73
Chapter 23: How to use a javascript library without a type definition file
.................................. 75
Section 23.1: Make a module that exports a default any
........................................................................................ 75
Section 23.2: Declare an any global
.......................................................................................................................... 75
Section 23.3: Use an ambient module
....................................................................................................................... 76
Chapter 24: Modules - exporting and importing
........................................................................................ 77
Section 24.1: Hello world module
............................................................................................................................... 77
Section 24.2: Re-export
.............................................................................................................................................. 77
Section 24.3: Exporting/Importing declarations
...................................................................................................... 79
Chapter 25: Why and when to use TypeScript
............................................................................................ 80
Section 25.1: Safety
..................................................................................................................................................... 80
Section 25.2: Readability
............................................................................................................................................ 80
Section 25.3: Tooling
................................................................................................................................................... 80
Chapter 26: Debugging
............................................................................................................................................ 81
Section 26.1: TypeScript with ts-node in WebStorm
................................................................................................ 81
Section 26.2: TypeScript with ts-node in Visual Studio Code
.................................................................................. 82
Section 26.3: JavaScript with SourceMaps in Visual Studio Code
.......................................................................... 83
Section 26.4: JavaScript with SourceMaps in WebStorm
....................................................................................... 83
Chapter 27: Arrays
..................................................................................................................................................... 85
Section 27.1: Finding Object in Array
......................................................................................................................... 85
Chapter 28: Typescript-installing-typescript-and-running-the-typescript-compiler-tsc
............................................................................................................................................................................................... 86
Section 28.1: Steps
....................................................................................................................................................... 86
Chapter 29: Configure typescript project to compile all files in typescript.
................................ 88
Section 29.1: Typescript Configuration file setup
..................................................................................................... 88
Chapter 30: Using Typescript with RequireJS
............................................................................................. 90
Section 30.1: HTML example using requireJS CDN to include an already compiled TypeScript file
.................. 90
Section 30.2: tsconfig.json example to compile to view folder using requireJS import style
............................. 90
Credits
.............................................................................................................................................................................. 91
You may also like
........................................................................................................................................................ 93
About
Please feel free to share this PDF with anyone for free,
latest version of this book can be downloaded from:
http://GoalKicker.com/TypeScriptBook
This
TypeScript Notes for Professionals
book is compiled from
Stack Overflow
Documentation,
the content is written by the beautiful people at Stack Overflow.
Text content is released under Creative Commons BY-SA, see credits at the end
of this book whom contributed to the various chapters. Images may be copyright
of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not
affiliated with official TypeScript group(s) or company(s) nor Stack Overflow. All
trademarks and registered trademarks are the property of their respective
company owners
The information presented in this book is not guaranteed to be correct nor
accurate, use at your own risk
Please send feedback and corrections to
web@petercv.com
TypeScript Notes for Professionals
1
Zgłoś jeśli naruszono regulamin