Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions pages/#8.tsx
Original file line number Diff line number Diff line change
@@ -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;