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

TypeScript メモ

基本的なデータ型と宣言方法

var a: string = ''
var b: number = 10
var c: boolean = false
var d1: string[] = ['hello', 'world', 'something']
var d2: number[] = [1, 3, 4]
var e: any //どんなデータ型でも格納可能

関数の書き方

//返り値がある場合
function add(a: number, b: number): number{
  return a + b
}

//返り値がない場合
function add(a: number, b: number): void{
  //処理内容記載...
}

通常の関数宣言だけでなく、式のように関数を書くことができる。

// 通常の関数式
var addNormal = function(a: number, b: number): number{
  return a + b
}

// アロー記法
var addArrow = (a:number, b:number): number => {
  return a + b
}

// ワンライナー記法
var addArrowOneLiner = (a: number, b: number): number => a + b

console.log(addNormal(6, 10))$
console.log(addArrow(60, 100))$
console.log(addArrowOneLiner(600, 1000))$

関数のオーバーロード

function add(a: number, b: number): number //引数と返り値の組み合わせをシグナチャと呼ぶ
function add(a: string, b: string): string

function add(a: any, b: any): any{
  if (typeof a === "string" && typeof b === "string"){
    return a + ' ' + b
  }
  return a + b
}

クラス

クラス定義
コンストラクタ
インスタンス化
動作確認

class User {
  constructor(public name: string){
    this.name = name
  }

  sayHello(): void{
    console.log('hello ' + this.name)
  }
}

Getter/Setter
Userのプロパティに対するアクセスを制限するためにprivate修飾子をつける。

class User {
  constructor(private _name: string){ //privateの変数には慣習的に「_」をつける
  }

  public sayHi(): void{
    console.log('hi! i am ' + this._name)
  }

  get name() {
    return this._name
  }

  set name(newValue: string){
    this._name = newValue
  }
}
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