0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Webアプリケーション開発 TypeScriptsとNext.js 勉強メモ(初級)

Posted at

var型にはスコープはないが,let型にはスコープがある

// var型の場合,エラーは発生しない
function calc(isSum: boolean) {
    let a = 100
    if (isSum)
    {
        var b = a + 1
        return b
    }
    return b
}

console.log(calc(true))

// let型の場合,スコープによるエラーが発生する
function calc(isSum: boolean) {
    let a = 100
    if (isSum)
    {
        let b = a + 1
        return b
    }
    return b
}

console.log(calc(true))

オブジェクト型について

{キー名: 型}

という形でオブジェクト型を定義できる

any

any型は全ての型を許容する型
特定の値に対して型チェックの仕組みを適用したくないとき場合に利用

let user: any = {firstname: 'Takuya' }
//以下の行はいずれの行もコンパイルエラーが発生しない
user.hello()
user()
user.age = 100
user = 'hello'

関数

関数の宣言方法は以下感じで

function (引数1: 型, 引数2, 型, .... ) : 戻り値 {
...
}

?をつけることでオプショナルな引数(指定しなくてもいい引数のこと)も指定できる

function sayGoodmorning (name: string, sentence?: string) string {
    return ${setence || ''} ${name}
}
sayGoodmorning('Takuya')
sayGoodmorning('Takuya', 'Goodmorning')
// 出力結果
Takuya
Goodmorning Takuya

デフォルト値の指定

function say(name: string, say: string = "nikoniko") string {
     return ${say} ${name}
}
say('Ayato')
// 出力結果
nikoniko Ayato
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?