?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.
ts-demo-project/
├── dist
# the compiled JS files
└── src
# for source TS files
mkdir -p ts-demo-project/{dist,src}
package.json
file, which holds various metadata relevant to the project and keeps track of your project's dependencies and scripts
# navigate to project root folder
cd ts-demo-project
# create package.json with defaults
npm init -y
npm install typescript --save-dev
# create the tsconfig.json with some default settings
tsc --init
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
helloUser.ts
in src/
folder
let userName:string = 'Ada';
let userAge:number = 34;
console.log(`Hello ${userName}, you are ${userAge} age old`);
tsc
dist
directory if you specified it in your tsconfig.json
tsc --watch
Reference: Everyday Types @typescriptlang.org
// Boolean
let isDone: boolean = true;
console.log(isDone);
// Number
let decimal: number = 6;
let hex: number = 0xf00d;
console.log(decimal, hex);
// String
let favColor: string = "black";
console.log(favColor);
// Array
let array1: number[] = [1, 2, 3];
let array2: Array<number> = [4, 5, 6, 7]; // Using generic array type
console.log(array1, array2);
// Tuple
let x: [string, number];
x = ["hello", 10];
console.log(x[0]); // Outputs: hello
console.log(x[1]); // Outputs: 10
// Enum
enum Color { Red, Green, Blue }
let c: Color = Color.Green;
console.log(c); // Outputs: 1 (the enum's value)
// Object
let someObject: object = { name: "someObject" };
console.log(someObject);
// Any
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
console.log(notSure);
// The function add takes two parameters x and y, both of type number, and returns a number.
function add(x: number, y: number): number {
return x + y;
}
console.log( add(2,3) ); // 5
console.log( add("hello", 5) ); // Error: Argument of type 'string' is not assignable to parameter of type 'number'.
console.log( add(5) ); // Error: Expected 2 arguments, but got 1.
console.log( add(2,3).toUpperCase() ); // Error: Property 'toUpperCase' does not exist on type 'number'.
// 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