2
0

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で値オブジェクトとして使えそうな文法調査ログ

Posted at

Clean Architecture の文脈でEntityに書くときに使えそうなものをメモとして書いておく。

tl;dr

とりあえずはclass使う。

classを使う

class Id {
    constructor(readonly value: number) {}
}

class MailAddress {
    constructor(readonly value: string) {}
}

class User {
    constructor(
        readonly id: Id,
        readonly mailAddress: MailAddress,
    ) {}
}

const u = new User(new Id(0), new MailAddress("hoge@fuga.foo"));

Playground Link

good

  • instanceofが使えるので型の特定が楽
  • 関数をもたせたりできるので柔軟性が高い

bad

  • 特に感じていないが、強いて言えばJavaっぽい。

interfaceを使う

const ID = "id";
type ID_TYPE = "id";
interface Id {
    readonly type: ID_TYPE;
    readonly value: number;
}

const MAIL_ADDRESS = "mail_address";
type MAIL_ADDRESS_TYPE = "mail_address";
interface MailAddress {
    readonly type: MAIL_ADDRESS_TYPE;
    readonly value: string;
}

const USER = "user";
type USER_TYPE = "user";
interface User {
    readonly type: USER_TYPE;
    readonly id: Id;
    readonly mailAddress: MailAddress;
}

const _id: Id = { type: ID, value: 10 };
const _mailAddress: MailAddress = { type: MAIL_ADDRESS, value: "hoge@fuga.foo" };
const u: User = {type: USER, id: _id, mailAddress: _mailAddress};

Playground Link

good

  • 生成後のjsがシンプル
  • Pickとかが使える

bad?

  • typeofとかを使うと単にプリミティブ型が出てきちゃうだけで、型の判定ができない。
    • あくまでトランスパイル時に型チェックが入るだけなので、実行時のでの判定ができない。
    • => そのため、typeという変数を入れている。

bad部分に関しては自信ないので他の方法があれば知りたい。

typeを使う

type Id = number;
type MailAddress = string;
type User = { id: Id, mailAddress: MailAddress};

const _id: Id = 20;
const _mailAddress: MailAddress = "hoge@fuga.foo";

const u: User = { id: _id, mailAddress: _mailAddress };

Playground Link

good

  • シンプル
    • valueとかいらない

bad?

  • type aliasがあるだけなので、型が違くても入れられちゃうケースがある。
    • もとがstringだったら何でも入る、みたいな感じ

bad部分に関しては自信ないので他の方法があれば知りたい。

まとめ

現時点ではclass使って全部定義しちゃうでいいかも。
でもオーバーヘッド掛かりそうでそこ気にしちゃうな。
あと、TypeScript Playground超便利

ツッコミマサカリは絶賛募集中です。

2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?