-
Basically TypeScript is a superset of JavaScript, with static Type analysis
-
3 big developer experiences / differentiators...
- Always up to date docs for functions and classes
- Fewer
undefined is not...an object/function errors - Refactoring is way easier one your application is typed
-
It is not painful to use!
-
TypeScript does infer types,
let,return,Object,Array -
anyis the great and terrible escape hatch -
What do I do with TypeScript?
-
Define your own
type(aliases),interface, andclassdefinitions to describe shape of data- Type: name your shape (no
extend,implements) - Interface: extensible shape using
extends - Class: use
implementsand/orextendfor both shape and behavior (methods)
- Type: name your shape (no
-
Start with
typealiases as a default, can be union or intersection types -
Use
interfacewhen many classes implement -
Define a
classwhen you need shape of data and aconstructor -
TypeScript is a structural type system -> just concerned with "shapes" of data
-
nullandundefinedare accounted for with "optional" notationprop?: string -
Turn on strict null checking
-
Use generics, e.g.
(arg: T) => T,Array<T>interface Identity<T> { (arg: T): T; },function identity<T>(arg: T): T { return arg; },let num: Identity<number> = identity;
-
Union Types
number | string | boolean -
Discriminated Union types
type Shape = Square | Rectangle | Circle;, all three interfaces have akindproperty (canswitchon that prop) -
Literal types
'active', -
Enum
enum Shapes { Circle, Rectangle, Square, }, -
Intersection types
Person & Serializable & Loggable
constructor(attrs) { super(attrs) }replacesinit- Use class (
static) + instance properties (assigned aftersupercall) - Prototype problems: use Decorators,
@tagName('ul')cheat-sheet - Favor new style
classdeclaration vsEmberObject.extend|create - Services use a type registry to declare module interface
- Ember Data: use
classandextendsDS.Model with decorators - Mirage factories, use
extendsno decorators, use registries (e.g. using Ember Data) - .hbs types? someone has been working on that (e.g. JSX w/ TS)
- Start with what's used most and has least dependendencies, or isolated feature