?print-pdf
' Създаден от
# install as global node module:
$ sudo npm install -g typescript
# check installation
$ tsc -v
Version 3.8.3
class Animal {
name:string
constructor(name) {
this.name = name;
}
eat(){
console.log(`${this.name} is eating`);
}
speak() {
console.log(`${this.name} makes an animal noise`);
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
speak() {
console.log(`${this.name} barks`);
}
}
var pluto = new Dog('Pluto');
pluto.speak();
pluto.eat();
tsc ES6toES5.ts
let add = function(x,y) {
console.log(x+y)
}
// this is legal JS call, but TypScript will raise an error:
add(3);
//test.ts(5,1): error TS2554: Expected 2 arguments, but got 1.
You have to create nex folder structure for your TypeScript Demo Project:
ts_demo_project/
├── dist
# the compiled JS files
└── src
# for source TS files
You can use the next bash one-liner:
mkdir -p ts_demo_project/{dist,src}
package.json
package.json
file holds various metadata relevant to the project.
cd ts_demo_project
npm init
package.json
Add the next script command to package.json
"scripts": {
"build": "tsc",
"start": "npm run build -- -w"
},
tsconfig.json
file
# make sure you are in the root project folder:
pwd
# .../ts_demo_project
# create the tsconfig.json with some default settings
tsc --init
or manually create the tsconfig.json
and paste into it
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": false,
"module": "commonjs",
"target": "ES5",
},
"exclude": [
"node_modules"
]
}
Just make sure, that you have "outDir": "./dist/"
helloUser.ts
in src/
folder
let userName='Ada';
let userAge=34;
console.log(`Hello ${userName}, you are ${userAge} age old`);
# make sure you're in the root project folder:
pwd
# start the project
npm start
src/
folder into respective file in dist/
folder. And that will happen on every change.Reference: Basic Types @typescriptlang.org
let start: boolean = false;
let age: number = 16;
let userName: string = "Ada";
let dataList: number[] = [1, 2, 3];
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
let anything: any = 42;
anything = "now I'm a string";
anything = false;
function showMessage(msg: string): void {
console.log(msg);
}
showMessage('Howdy!');
function add(x: number, y: number): number {
return x + y;
}
console.log( add(2,3) );
// optional parameters:
function greet(firstName: string, lastName?: string) {
return lastName? `${firstName} ${lastName}`: firstName;
}
console.log( greet('Ada', 'Byron') );
console.log( greet('Ada') );
These slides are based on
customised version of
framework