sudo npm install -g typescript
function log(message) { console(message); } let message = 'Hello Typescript'; log(message);
main.ts
and, going to the terminal where you saved it, run the following command: tsc main.ts
main.js
, which is a transpiled JS version of the main.ts
file. This file can, for example, be executed using Node.js: node main.js
tsc
command when building the Angular application, since the ng serve
tool will prepare the code for execution automatically. let a: number //: 1, 2, 3 let b: boolean //: true, false let c: string //: "abel agoi" let d: any // let e: number[] // , : [1, 3, 54] let f: any[] // , : [1, "abel agoi", true]
enum
, you can read about it yourself. let log = function (message) { console.dir(message); }
=>
. Here's what it looks like: let log = (message) => { // function console.dir(message); } // , let log = (message) => console.dir(message); // , let log = message => console.dir(message); //,
let myFunction = ( a: number, b: number, c: string, d: any, e: number[], f: any[] ) => { console.log('something); }
interface MyParameters { a: number, b: number, c: string, d: any, e: number[], f: any[] ... ... } let myFunction = (myParameters: MyParameters) { }
myPoints.ts
and put the following code into it: class MyPoint { x: number; y: string; draw() { // , x this console.log("X is: " + this.x); console.log("X is: " + this.y); } getDistanceBtw(another: AnotherPoint) { // } }
let myPoint = new MyPoint() //MyPoint - myPoint.x = 2; // x myPoint.y = "a"; // y myPoint.draw(); // draw
myPoints.ts
can be myPoints.ts
and run what happened: tsc myPoint.ts | node myPoint.js
let myPoint = new MyPoint() myPoint.x = 2; myPoint.y = "a";
let myPoint = new MyPoint(2, "a");
class MyPoint { x: number; y: string; constructor (x: number, y: string) { this.x = x; this.y = y; } draw() { // , x this console.log("X is: " + this.x); console.log("X is: " + this.y); } getDistanceBtw(another: AnotherPoint) { // } }
?
) In the constructor. This sign allows you to define parameters that, when creating an instance of a class, are optional: class MyPoint { x: number; y: string; constructor (x?: number, y?: string) { // "?" ":" this.x = x; this.y = y; } draw() { // , x this console.log("X is: " + this.x); console.log("X is: " + this.y); } getDistanceBtw(another: AnotherPoint) { // } } // myPointA MyPoint let myPointA = new MyPoint() myPoint.x = 2; myPoint.y = "a"; myPoint.draw(); // myPointB MyPoint let myPointB = new MyPoint(2, "b"); myPointB.draw(); // myPoint MyPoint let myPointC = new MyPoint(2); // , Y myPointC.draw();
public
, protected
and private
. By default, all class members are public - this is similar to using the public
access modifier with them, that is, they can be read and modified externally. Using the access modifier private
allows you to prevent external mechanisms from working with class members. For example, here we used this modifier with x
and y
properties: class MyPoint { ... private x: number; private y: string; // public draw() { // - } ... } let myPoint = new MyPoint(); myPoint.x = 3;
myPoint.x
construct when working with an instance of a class will result in an error, because the property is declared with the access modifier private
. private x: number; public y: string; constructor (x: number, y: string) { this.x = x; this.y = y; }
constructor (private x: number, public y: string) {}
private x: number; public y: string; this.x = x; this.y = y;
MyPoint
class looks like this: class MyPoint { constructor (private x?: number, private y?: string) {} draw() { // - } drawAnotherThing() { // - } }
x
and y
properties outside the instance of the MyPoint
class, since they are declared using the access modifier private
. If you need to somehow influence them or read their values from the outside, we will need to use getters (for reading properties) and setters (for their modification). Here's what it looks like: class MyPoint { constructor (private x?: number, private y?: string) {} getX() { // X return this.x; } setX(value) { // X this.x = value; } } // x , myPoint // setX() X let myPoint = new MyPoint(); myPoint.setX = 4; console.log( myPoint.getX() ); // 4;
myPoint.setX()
to set the value of x
, what if you could do something like this: myPoint.X = 4; // X , ,
get
and set
keywords in front of the function name, that is, comparing it with the previous examples, separate these words with a space from the function name following them: class MyPoint { constructor (private x?: number, private y?: string) {} get X() { // X return this.x; } set X(value) { // Y this.x = value; } }
_
): class MyPoint { constructor (private _x?: number, private _y?: string) {} get x() { // X return this._x; } set x(value) { // Y this._x = value; } }
myPoint.ts
file to the myPoint.ts
form: export class MyPoint { // export constructor (private _x?: number, private _y?: string) {} get x() { return this._x; } set x(value) { // x this._x = value; } draw() { // - } }
export
keyword, the class described in the MyPoint.ts
file can be made visible in other files by importing it into them using the import
keyword.MyPoint
class in the MyPoint
file, it must be imported: import { MyPoint } from './myPoint'; class Main { let MyPoint = new MyPoint(4, "go to go"); MyPoint.draw(); }
main.ts
and myPoint.ts
are in the same directory.Source: https://habr.com/ru/post/344502/
All Articles