TypeScript Classes
TypeScript Classes
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.
id: string;
// A field
class Location {
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);
inside the JavaScript engine that it set ame(name: string) { t is.name = name }
is only accessible inside the class: verifyName = (name: string) = > { ... }
function fields)
"
console.log( Hello , " + h
t is.get ame())N ;
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<{ ... }> { ... }
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"
}
const a:Bag = new Bag()
@triggers Sync()
Generics class Bo
contents: Type
So, be careful to not do this: Declare a type which can constructor(value: Type) {
@pre ferCache(false)
methods.
}
Used here
}
update(@required f
in o: Partial<User>) { ... }