Skip to content

Latest commit

 

History

History
72 lines (42 loc) · 1.79 KB

basics.md

File metadata and controls

72 lines (42 loc) · 1.79 KB

Basics

References

Static Type-checking

Ideally, we could have a tool that helps us find these bugs before our code runs. That's what a static type-checker like TypeScript does.

Static types systems describe the shapes and behaviors of what our values will be when we run our programs. A type-checker like TypeScript uses that information and tells us when things might be going off the rails.

Non-exception Failures

……

Types for Tooling

……

tsc, TypeScript Compiler

Emitting with Errors

tsc --noEmitOnError hello.ts

Explicit Types

function greet(person: string, date: Date) {
    console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}

greet("Maddison", new Date());

In many cases, TypeScript can even just infer (or "figure out") the types for us even if we omit them.

let msg = "hello there!";

Erased Types

……

Downleveling

By default TypeScript targets ES3.

The great majority of current browsers support ES2015. Running with --target es2015 changes TypeScript to target ECMAScript 2015, meaning code should be able to run wherever ECMAScript 2015 is supported.

Most developers can therefore safely specify ES2015 or above as a target, …

Strictness

Some people are looking for a more loose opt-in experience which can help validate only some parts of their program, and still have decent tooling.

"strict": true in a tsconfig.json toggles …

noImplicitAny

most lenient type any

strictNullChecks

makes handling null and undefined more explicit, and spares us from worrying about whether we forgot to handle null and undefined.