Christian Köberl / christian.koeberl@porscheinformatik.at
[] + {} |
==== |
"[object Object]" |
{} + [] |
==== |
0 |
1 + {} |
==== |
"1[object Object]" |
{} + 1 |
==== |
1 |
1 + {} + 1 |
==== |
"1[object Object]1" |
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
let person = new Person(1, 2);
person.mama = 'Mia';
person = Math.sqrt(person);
export class Person {
firstName: string;
lastName: string;
dateOfBirth?: Date;
constructor(firstName: string, lastName: string) {
this.firstName = firstName;
this.lastName = lastName;
}
toString() {
return `${this.firstName} ${this.lastName} (${this.dateOfBirth})`;
}
}
let hello: string = 'Hello';
hello = 5; // error
class Person {
constructor(private firstName: string, private lastName: string) {
}
getName(): string {
return `${this.firstName} ${this.lastName}`;
}
}
let person = new Person("Chris", "Köberl");
person.getName(); // Chris Köberl
class Employee extends Person {
employeeNo: number;
constructor(firstName: string, lastName: string,
employeeNo: number) {
super(firstName, lastName);
this.employeeNo = employeeNo;
}
getName() {
return super.getName() + '(' + this.employeeNo + ')';
}
}
interface Person {
firstName: string;
lastName: string;
dateOfBirth?: Date;
}
class Employee implements Person {
constructor(public firstName: string, public lastName: string) {
}
}
let person: Person = {
firstName: 'Chris',
lastName: 'Kö'
};
person.dateOfBirth = new Date();
function savePerson(p: Person) {
// ...
}
let name = 'Christian'; // no type needed
// even no types for complex stuff
document
.getElementById('id')
.addEventListener('click',
ev => alert(`${ev.clientX}, ${ev.clientY}`));
class Stack<T> {
private items: T[] = [];
public push(item: T) {
this.items.push(item);
}
public pop(): T {
return this.items.pop();
}
}
let stack = new Stack<string>();
stack.push("a");
let a = stack.pop();
d.ts
$('#id').toggleClass('class');
interface JQuery {
toggleClass(className: string): JQuery;
}
interface JQueryStatic {
(selector: string): JQuery;
}
declare var $: JQueryStatic;
export
and import
class MyClass {
@measure()
doSomething() {
// ...
}
}
function measure() {
return function (target: any, propertyKey: string,
descriptor: PropertyDescriptor) {
let originalFunction = descriptor.value;
descriptor.value = function (...args: any[]) {
let start = Date.now();
originalFunction.apply(target, args);
console.log(`${propertyKey} with params "${args}"
took ${Date.now() - start}ms`);
}
}
}
doSomething() {
return getToken().then(token => {
return loadData(token);
}).then(data => {
this.data = data;
});
}
async doSomething() {
let token = await getToken();
this.data = await loadData(token);
}
Install: npm i (-g|-D) typescript
Init/config: tsc --init
» tsconfig.json
Run: tsc
{
"compileOnSave": false,
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"outDir": "dist",
"types": [ "node", "jasmine" ]
},
"include": [ "src/**/*" ]
}
Install: npm install typings --global
Search: typings search jasmine
Install: typings install @types/jasmine --save
Install from Definitely Typed: typings install dt~angular --global --save
Install: npm i (-g|-D) tslint
Init/config: tslint --init
» tslint.json
Run: tslint -c tslint.json 'src/**/*.ts'
readonly
attributes@@deprecated
)