2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TypeScriptの宣言空間 namespace / type / value のメモ

Posted at

宣言空間

namespace / type / valueがあり、それぞれ宣言空間が異なるので、同じ変数名で定義してもエラーにならない

// 競合しない型・値の宣言
const Test = {} // OK
interface Test {} // OK
namespace Test {} // OK

value宣言空間

const val1 = 'test'
// let val1 = 'guest' ・・同じvalue宣言空間なのでエラーになる

type宣言空間

・type alias or interfaceを使う
・interface -> open endedに準拠しているため「型拡張(オーバーロード)」可能

// 同じ名前でもコンパイルエラーにならない
interface Peter {
    name: string
}
interface Peter {
    age: number
}
interface Peter {
    weight: number
}

・type -> open endedに準拠してないため型拡張は出来ない

type Pan = { // コンパイルエラー
    name: string
}
type Pan = { // コンパイルエラー
    age: number
}

namespace宣言空間

// namespaceの中でinterfaceを定義してexportする
namespace Test1 {
    export interface Props {
        name: string
    }
}

// Tes1.PropsとすることでNamespace内の型を使える 
const properties: Test1.Props = {
    name: 'PropA'
}
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?