メインコンテンツまでスキップ

constructor shorthand

メソッドの引数にはアクセス修飾子を設定することはできませんがコンストラクタは特別です。

引数に対してアクセス修飾子を宣言した場合はこのような意味になります。

アクセス修飾子説明
(宣言なし)constructorメソッド内のみアクセス可能
public自身のクラス内、継承クラス、インスタンス化されたクラスのどれからでもアクセス可能
protected自身のクラス、継承クラスからアクセス可能
private自身のクラスのみアクセス可能

ConstructorInAccessModifierクラスとConstructorOutAccessModifierクラスのふたつを定義しました。

ふたつのクラスの違いはコンストラクタにアクセス修飾子を定義しているかどうかだけで機能はまったく同じです。

example.ts
ts
class ConstructorInAccessModifier {
constructor(
arg0: number,
public arg1: number,
protected arg2: number,
private arg3: number
) {
console.log({ arg0, arg1, arg2, arg3 });
}
}
 
class ConstructorOutAccessModifier {
public arg1: number;
protected arg2: number;
private arg3: number;
constructor(arg0: number, arg1: number, arg2: number, arg3: number) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
console.log({ arg0, arg1, arg2, arg3 });
}
}
example.ts
ts
class ConstructorInAccessModifier {
constructor(
arg0: number,
public arg1: number,
protected arg2: number,
private arg3: number
) {
console.log({ arg0, arg1, arg2, arg3 });
}
}
 
class ConstructorOutAccessModifier {
public arg1: number;
protected arg2: number;
private arg3: number;
constructor(arg0: number, arg1: number, arg2: number, arg3: number) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
console.log({ arg0, arg1, arg2, arg3 });
}
}

コンパイル後のJavaScriptファイルを見てみると同一の機能を持つことが確認することができます。

example.js
js
class ConstructorInAccessModifier {
constructor(arg0, arg1, arg2, arg3) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
console.log({ arg0, arg1, arg2, arg3 });
}
}
class ConstructorOutAccessModifier {
constructor(arg0, arg1, arg2, arg3) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
console.log({ arg0, arg1, arg2, arg3 });
}
}
example.js
js
class ConstructorInAccessModifier {
constructor(arg0, arg1, arg2, arg3) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
console.log({ arg0, arg1, arg2, arg3 });
}
}
class ConstructorOutAccessModifier {
constructor(arg0, arg1, arg2, arg3) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
console.log({ arg0, arg1, arg2, arg3 });
}
}

TypeScriptで記述する際は各アクセス修飾子のスコープ機能が有効になるため、インスタンスからのアクセスが可能なプロパティはpublic宣言されたarg1のみが有効になります。

example.ts
ts
const InAccess = new ConstructorInAccessModifier(1, 2, 3, 4);
InAccess.arg0;
Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.
InAccess.arg1;
InAccess.arg2;
Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.
InAccess.arg3;
Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.
 
const outAccess = new ConstructorOutAccessModifier(1, 2, 3, 4);
outAccess.arg0;
Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.
outAccess.arg1;
outAccess.arg2;
Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.
outAccess.arg3;
Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.
example.ts
ts
const InAccess = new ConstructorInAccessModifier(1, 2, 3, 4);
InAccess.arg0;
Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.
InAccess.arg1;
InAccess.arg2;
Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.
InAccess.arg3;
Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.
 
const outAccess = new ConstructorOutAccessModifier(1, 2, 3, 4);
outAccess.arg0;
Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.
outAccess.arg1;
outAccess.arg2;
Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.
outAccess.arg3;
Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.

つまり、コンストラクタの引数のアクセス修飾子はプロパティ宣言の省略をしてくれるだけにすぎません。

  • 質問する ─ 読んでも分からなかったこと、TypeScriptで分からないこと、お気軽にGitHubまで🙂
  • 問題を報告する ─ 文章やサンプルコードなどの誤植はお知らせください。