Help us understand the problem. What is going on with this article?

typescritpt for begginer with mosh

  • Typscript → Javascript
let a: number;
let c:string;
let e:number[] = [1,2,3];


enum Color { Red = 0 , Green = 1};

const backgroundColor = Color.Red;

console.log(a)

  • objectはinterfaceでまとめてしまう
アロー関数とinterface
let drawPoint = (point: {x: number, y:number}) =>{
    //..
}

drawPoint({
    x: 1,
    y: 2 
})



# interface

interface Point {
  x: number,
  y: number
}

let drawPoint = (point: Point) =>{
    //..
}

drawPoint({
    x: 1,
    y: 2 
})
クラス

class Point {
  x: number;
  y: number;

  draw(){
    console.log('X: ' + this.x + ', Y: ' + this.y)
  }

  getDistance(another: Point){

  }

}



let point = new Point();
point.x = 1;
point.y = 2;

point.draw()


tsc main.ts && node main.js

コンストラクターあり

class Point {
  x: number;
  y: number;

  constructor(x: number, y: number){
    this.x = x
    this.y = y
  }

  draw(){
    console.log('X: ' + this.x + ', Y: ' + this.y)
  }

  getDistance(another: Point){

  }

}



let point = new Point(1,2);

point.draw()

  • Access Modifier when you update property
アクセッサ

class Point {
  constructor(private x: number,private y: number){
  }

  draw(){
    console.log('X: ' + this.x + ', Y: ' + this.y)
  }

  getX(){
    return this.x;
  }

  setX(value){
    if (value < 0) 
       throw new Error('value caoonot be less than  0')
     this.x = value;
  }

}



let point = new Point(1,2);

let x = point.getX()
point.setX(10)

point.draw()


## これが最新

- x,y  _x  _y


class Point {
  constructor(private _x: number,private _y: number){
  }

  draw(){
    console.log('X: ' + this._x + ', Y: ' + this._y)
  }

  get x(){
    return this.x;
  }

  set x(value){
    if (value < 0) 
       throw new Error('value caoonot be less than  0')
     this._x = value;
  }

}



let point = new Point(1,2);

let x = point.x
point.x = 10;

point.draw()

  • module
/point.ts

export class Point{

}

/main.ts

import {Point} from './Point'

let point = new Point(1,2)
point.draw()
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away