From 3c925b0c0cf43a09e1d5535096caa4bf15e9e2b3 Mon Sep 17 00:00:00 2001 From: keita namazue Date: Sun, 20 Mar 2022 19:16:59 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20=20TypeScript=E7=AC=AC8?= =?UTF-8?q?=E5=9B=9E=E8=AC=9B=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/#8.tsx | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 pages/#8.tsx diff --git a/pages/#8.tsx b/pages/#8.tsx new file mode 100644 index 0000000..90fa46e --- /dev/null +++ b/pages/#8.tsx @@ -0,0 +1,112 @@ +/*==================================================== + +【TypeScript入門 #8】Intersection Types(交差型)、Union Types(共用体型・合併型) + +====================================================*/ + +/*==================================================== + +Intersection Types(交差型) + +====================================================*/ + +// type Foo = { +// a: number; +// b: string; +// }; + +// type Bar = { +// c: boolean; +// }; + +// type FooBar = Foo & Bar; + +// const Test: FooBar = { +// a: 1, +// b: "2", +// c: true, +// }; + +/*==================================================== + +Intersection Typesとプリミティブ型 + +====================================================*/ + +// type Foo = string; +// type Bar = number; + +// type FooBar = Foo & Bar; + +/*==================================================== + +Union Types(共用体型・合併型) + +====================================================*/ + +// type Foo = { +// a: number; +// b: string; +// }; + +// type Bar = { +// c: boolean; +// }; + +// type FooBar = Foo | Bar; + +// const test01: FooBar = { +// c: true, +// }; + +// const test02: FooBar = { +// a: 1, +// b: "2", +// c: true, +// }; + +/*==================================================== + +Union Typesの絞り込み(in演算子) + +====================================================*/ + +// type Foo = { +// a: number; +// b: string; +// }; + +// type Bar = { +// a: string; +// c: boolean; +// }; + +// type FooBar = Foo | Bar; + +// const test03: FooBar = { +// a: 1, +// b: "2", +// c: true, +// }; + +// if ("b" in test03) { +// test03.a.toFixed(); +// } else { +// test03.a.toString(); +// } + +/*==================================================== + +Union Typesとプリミティブ型 + +====================================================*/ + +// type Foo = number; +// type Bar = string; + +// type FooBar = Foo | Bar; + +// type Foo = 0; +// type Bar = "hello"; + +// type FooBar = Foo | Bar;