TypeScript
Class
A TypeScript class has a few type-specific extensions to ES2015 JavaScript These features are TypeScript specific language extensions which may
Key points
Cheat Sheet classes, and one or two runtime additions. never make it to JavaScript with the current syntax.
Creating an class instance Ensures that the class
Parameter Properties
Common Syntax Subclasses this class conforms to a set of
class ABC { ... }
interfaces or types A TypeScript specific extension to classes which
const abc = new ABC() automatically set an instance field to the input parameter.
class User extends Account implements Updatable, Serializable {
id: string;
// A field
class Location {
Parameters to the new ABC come
constructor(public x: number, public u
from the constructor function. displayName?: boolean;
// An optional field
y: n mber) {}
name!: string;
// A ‘trust me, it’s there’ field
}
private x vs #private
#attributes: Map<any, any>;
// A private field
const loc = new Location(20, 40);
The prefix private is a type-only
roles = ["user"];
// A field with a default
loc.x // 20
addition, and has no effect at loc.y // 40
readonly createdAt = new Date()
// A readonly field with a default
runtime. Code outside of the class
can reach into the item in the
following case:
u
constr ctor(i : stringd , email: string) {
The code called on ‘new’
class Bag {
u
s per(i )d ;
Abstract Classes
private item: any
this.email = email ;
In strict: true this code is checked against
} ...
the fields to ensure it is set up correctly A class can be declared as not implementable, but as existing to
be subclassed in the type system. As can members of the class.
};
Vs #private which is runtime abstract class Animal {
private and has enforcement
N h W ays to describe class N
abstract get ame(): string ;
inside the JavaScript engine that it set ame(name: string) { t is.name = name }
methods (and arrow N
print ame() {
is only accessible inside the class: verifyName = (name: string) = > { ... }
function fields)
"
console.log( Hello , " + h
t is.get ame())N ;
class Bag { #item: any }
A function with 2
}
sync(): Promise<{ ... }>
overload definitions }
sync(cb: ((result: string) => void)): void
‘this’ in classes
The value of ‘this’ inside a function sync(cb?: ((result: string) => void)): void | Promise<{ ... }> { ... }
class Dog extends N
Animal { get ame(): { ... } }
depends on how the function is
called. It is not guaranteed to
always be the class instance which u ID() { }
get acco nt Getters and setters
you may be used to in other
languages.
set accountID(value: string) { }
Decorators and Attributes
k R quest() { ... ri ate access is just to this class, protected
P v
You can use decorators on classes, class methods, accessors, property and
You can use ‘this parameters’, use private ma e e }
allows to subclasses. Only used for type
protected handleRequest() { }
parameters to methods.
the bind function, or arrow ... checking, public is the default.
functions to work around the issue import {
when it occurs.
u u 0;
Syncable, triggersSync, f
pre erCac e h , re quired
static # serCo nt =
Static fields / methods
static registerUser(user: User) { ... }
} from "mylib"
Type and Value
Surprise, a class can be used as
both a type or a value. h
static { t is.# serCo nt =u u -1 }
Static blocks for setting up static @Syncable
vars. ‘this’ refers to the static class
class User {
}
const a:Bag = new Bag()
@triggers Sync()
Type Value x<Type> {
Class type parameter save() { ... }
Generics class Bo
contents: Type
So, be careful to not do this: Declare a type which can constructor(value: Type) {
@pre ferCache(false)
class C implements Bag {}
change in your class
this.contents = value;
get displayName() { ... }
methods.
}
Used here
}
update(@required f
in o: Partial<User>) { ... }
const stringBo x x "
= new Bo ( a pac age ) k " }