- Typescript
- TSLint
- WebPack
- Node server run with Express
Before building or running project, download dependencies by running following command in project root folder:
npm i
Before running project, compile and bundle source code with following command in project root folder:
npm run build
Run project by running following command in project root folder:
npm start
This starts the server. Navigate to localhost:8000
in favorite web browser to view the page.
Run unit tests with following command in project root folder:
npm test
When function is defined by a single expression is this:
const f = (x: string): number => {
return x.length;
};
can be simplefied into this:
const f = (x: string): number => x.length;
A normal function with body
function f() {
}
can be simplefied into this:
const f = () => {
}
Typescript file maintest.spec.ts
with following code
import { expect } from "chai";
import { f } from "./main";
describe("f", () => {
it("Should return string length 5", () => {
const actual = f("Bjorn");
const expected = 5;
expect(actual).to.equal(expected);
});
});
... runs code from file main.ts
:
export const f = (x: string): number => 4;
const obj = { a: 1, b: "hej", c: true };
// copy obj to obj2:
const obj2 = { a: obj.a, b: obj.b; c: obj.c };
can be simplified with the ...
operator (the "spread" operator):
const obj = { a: 1, b: "hej", c: true };
// copy obj to obj2:
const obj2 = { ...obj };
const x = 5;
const obj = { x: x };
can be simplified with shorthand notation when field has the same name as the variable:
const x = 5;
const obj = { x };
const obj = { a: 0, b: 1, c: 2 };
const a = obj.a;
const b = obj.b;
can be simplified into this:
const obj = {a: 0, b: 1, c: 2 };
const { a, b } = obj;
type MyObj = { a: number, b: number };
const f = (obj: MyObj): number => obj.a + obj.b;
can be simplified into this:
type MyObj = { a: number, b: number };
const f = ({ a, b }: MyObj): number => a + b;
type ChildrenName = "Bjorn" | "Nanna" | "Alice" | "Elsie";
const f = (s: ChildrenName): void => {};
// Will not even compile:
f("knatte");
// Compiles correctly:
f("Nanna");
type MyType = {
a: "knatte";
} | {
a: "fnatte";
b: number;
} | {
a: "tjatte";
b: string;
};
const obj: MyType = // ... some implementation
// switch with common occuring field:
switch (obj.a) {
case "knatte":
// Will not even compile:
console.log(obj.b) // we can't get to this point with 'b'
break;
case "fnatte":
// Type of obj has been "narrowed" to { a: "fnatte", b: number }
console.log(obj.b) // prints out a number
break;
case "tjatte":
// Type of obj has been "narrowed" to { a: "tjatte", b: string }
console.log(obj.b) // prints out a string
break;
}