Tackling TypeScript

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Rauschmayer, Axel
Year: 2020

Language: English
Pages: 210
Tags: TypeScript

I Preliminaries
About this book
Where is the homepage of this book?
What is in this book?
What do I get for my money?
How can I preview the content?
How do I report errors?
What do the notes with icons mean?
Acknowledgements
Why TypeScript?
The benefits of using TypeScript
The downsides of using TypeScript
TypeScript myths
Free resources on TypeScript
II Getting started with TypeScript
How does TypeScript work? The bird's eye view
The structure of TypeScript projects
Programming TypeScript via an integrated development environment (IDE)
Other files produced by the TypeScript compiler
Using the TypeScript compiler for plain JavaScript files
Trying out TypeScript
The TypeScript Playground
TS Node
Notation used in this book
Test assertions (dynamic)
Type assertions (static)
The essentials of TypeScript
What you'll learn
Specifying the comprehensiveness of type checking
Types in TypeScript
Type annotations
Type inference
Specifying types via type expressions
The two language levels: dynamic vs. static
Type aliases
Typing Arrays
Function types
Union types
Optional vs. default value vs. undefined|T
Typing objects
Type variables and generic types
Example: a type-parameterized class
Conclusion: understanding the initial example
Creating CommonJS-based npm packages via TypeScript
Required knowledge
Limitations
The repository ts-demo-npm-cjs
.gitignore
.npmignore
package.json
tsconfig.json
TypeScript code
Creating web apps via TypeScript and webpack
Required knowledge
Limitations
The repository ts-demo-webpack
package.json
webpack.config.js
tsconfig.json
index.html
main.ts
Installing, building and running the web app
Using webpack without a loader: webpack-no-loader.config.js
Strategies for migrating to TypeScript
Three strategies
Strategy: mixed JavaScript/TypeScript code bases
Strategy: adding type information to plain JavaScript files
Strategy: migrating large projects by snapshot testing the TypeScript errors
Conclusion
III Digging deeper
What is a type in TypeScript? Two perspectives
Three questions for each perspective
Perspective 1: types are sets of values
Perspective 2: type compatibility relationships
Nominal type systems vs. structural type systems
Further reading
TypeScript enums: How do they work? What can they be used for?
The basics
Specifying enum member values (advanced)
Downsides of numeric enums
Use cases for enums
Enums at runtime
const enums
Enums at compile time
Acknowledgment
Alternatives to enums in TypeScript
Unions of singleton values
Discriminated unions
Object literals as enums
Enum pattern
Summary of enums and enum alternatives
Acknowledgement
Adding special values to types
Adding special values in band
Adding special values out of band
Typing objects
Roles played by objects
Types for objects
Object vs. object in TypeScript
Object type literals and interfaces
Type inference
Other features of interfaces
JavaScript's prototype chains and TypeScript's types
Sources of this chapter
Class definitions in TypeScript
Cheat sheet: classes in plain JavaScript
Non-public data slots in TypeScript
Private constructors
Initializing instance properties
Abstract classes
Class-related types
The two prototype chains of classes
Interfaces for instances of classes
Interfaces for classes
Classes as types
Further reading
Types for classes as values
Types for specific classes
The type operator typeof
A generic type for classes: Class
Typing Arrays
Roles of Arrays
Ways of typing Arrays
Pitfall: type inference doesn't always get Array types right
Pitfall: TypeScript assumes indices are never out of bounds
Typing functions
Defining statically typed functions
Types for functions
Parameters
Overloading (advanced)
Assignability (advanced)
Further reading and sources of this chapter
IV Dealing with ambiguous types
The top types any and unknown
TypeScript's two top types
The top type any
The top type unknown
Type assertions (related to casting)
Type assertions
Constructs related to type assertions
Type guards and assertion functions
When are static types too general?
Narrowing via built-in type guards
User-defined type guards
Assertion functions
Quick reference: user-defined type guards and assertion functions
Alternatives to assertion functions
@hqoss/guards: library with type guards
Validating external data
JSON schema
Approaches for data validation in TypeScript
Example: validating data via the library Zod
Conclusion