From fa7b923cfe9196728fa46481286dd9d75851cb83 Mon Sep 17 00:00:00 2001 From: Shingo Yamazaki Date: Wed, 16 Jan 2019 00:43:45 +0900 Subject: [PATCH 1/4] Our First Interface --- .../src/interfaces/our-first-interface.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 typescript-handbook/src/interfaces/our-first-interface.ts diff --git a/typescript-handbook/src/interfaces/our-first-interface.ts b/typescript-handbook/src/interfaces/our-first-interface.ts new file mode 100644 index 0000000..6fc1c51 --- /dev/null +++ b/typescript-handbook/src/interfaces/our-first-interface.ts @@ -0,0 +1,26 @@ +/** + * Our First Interface + */ + +/* interface を使わない例 */ +function printLabel(labelledObj: { label: string }) { + console.log(labelledObj.label); +} + +// `label` があるかどうかだけをチェックするので、 +// 他のプロパティが含まれていてもエラーにならない +let myObj = { size: 10, label: 'Size 10 Object' }; +printLabel(myObj); + +/* interface を使った例 */ +interface LabelledValue { + label: string; +} + +function printLabel2(labelledObj: LabelledValue) { + console.log(labelledObj.label); +} + +// interface に定義されているプロパティがあるかだけをチェックする +let myObj2 = { size: 10, label: 'Size 10 Object' }; +printLabel2(myObj2); From 4a48e2e6d899c13f2bff466d7a330b8dea4d5578 Mon Sep 17 00:00:00 2001 From: Shingo Yamazaki Date: Wed, 16 Jan 2019 00:47:46 +0900 Subject: [PATCH 2/4] Optional Properties --- .../src/interfaces/optional-properties.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 typescript-handbook/src/interfaces/optional-properties.ts diff --git a/typescript-handbook/src/interfaces/optional-properties.ts b/typescript-handbook/src/interfaces/optional-properties.ts new file mode 100644 index 0000000..8b3f6ea --- /dev/null +++ b/typescript-handbook/src/interfaces/optional-properties.ts @@ -0,0 +1,22 @@ +/** + * Optional Properties + */ + +interface SquareConfig { + color?: string; + width?: number; +} + +function createSquqre(config: SquareConfig): {color: string; area: number} { + let newSquare = {color: 'white', area: 100}; + if (config.color) { + newSquare.color = config.color; + } + + if (config.width) { + newSquare.area = config.width * config.width; + } + return newSquare; +} + +let mySquare = createSquqre({color: 'black'}); From 9d469d1f0626dd26511d505ebfcc76b3986257f8 Mon Sep 17 00:00:00 2001 From: Shingo Yamazaki Date: Wed, 16 Jan 2019 00:53:16 +0900 Subject: [PATCH 3/4] ReadOnly properties --- .../src/interfaces/readonly-properties.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 typescript-handbook/src/interfaces/readonly-properties.ts diff --git a/typescript-handbook/src/interfaces/readonly-properties.ts b/typescript-handbook/src/interfaces/readonly-properties.ts new file mode 100644 index 0000000..135e40e --- /dev/null +++ b/typescript-handbook/src/interfaces/readonly-properties.ts @@ -0,0 +1,20 @@ +/** + * Readonly properties + */ +interface Point { + readonly x: number; + readonly y: number; +} + +let p1: Point = { x: 10, y: 20 }; +p1.x = 5; // error! + +let a: number[] = [1, 2, 3, 4]; +let ro: ReadonlyArray = a; +ro[0] = 12; // error! +ro.push(5); // error! +ro.length = 100; // error! +a = ro; // error! + +// readonly vs const +// variables には const, properties には readonly を使う From 86d2f475c1a55bf21da9710ea4af81ca09087cc8 Mon Sep 17 00:00:00 2001 From: shingo-yamazaki Date: Wed, 23 Jan 2019 22:23:02 +0900 Subject: [PATCH 4/4] Excess Property Checks --- .../src/interfaces/excess-property-checks.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 typescript-handbook/src/interfaces/excess-property-checks.ts diff --git a/typescript-handbook/src/interfaces/excess-property-checks.ts b/typescript-handbook/src/interfaces/excess-property-checks.ts new file mode 100644 index 0000000..4499ac1 --- /dev/null +++ b/typescript-handbook/src/interfaces/excess-property-checks.ts @@ -0,0 +1,25 @@ +/** + * Excess Property Checks + */ +interface SquareConfig { + color?: string; + width?: number; + // [propName:string]: any; +} + +function createSquare(config: SquareConfig): { color: string; area: number } { + + // ... +} + +// error +let mySquare = createSquare({ colour: 'red', width:100 }); + +// 回避方法1 +let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig); + +// 回避方法2 は、 SquareConfig に [propName:string]: any を追加する + +// 回避方法3 +let squreOptions = { colour: 'red', width:100 }; +let mySquare = createSquare(squreOptions); \ No newline at end of file