type UserId = number; type PostId = number; const userId: UserId = 888; const postId: PostId = 24; const register = (userId: UserId, postId: PostId) => {...} register(postId, userId) // 引数の順番をミスっているが、NoError!! type UserId = number & { "UserId": never }; type PostId = number & { "PostId": never }; type Branded = Type & { [key in Data]: never }; type UserId = Branded; const makeUserId = (n: number) => n as UserId; // 型の定義 type Branded = Type & { [key in Data]: never }; type UserId = Branded; type PostId = Branded; // id生成用関数を定義 export const makeUserId = (n: number) => n as UserId; export const makePostId = (n: number) => n as PostId; // idを生成 const userId = makeUserId(888); const postId = makePostId(24); const register = (userId: UserId, postId: PostId) => {}; register(postId, userId); // error! // idを生成 const userId = makeUserId(888); const postId = 24; // makePostIdをしていない register(userId, postId); // error! const userId = makeUserId(888) as PostId; // error! const userId = makeUserId(888); const a = userId + userId // number type Id = number; type UserId = Id<"UserId">; type PostId = Id<"PostId">; const userId = 24 as UserId; // number。この時点でムリってる const postId = 24 as PostId; // number const register = (userId: UserId, postId: PostId) => {}; register(userId, postId); // 引数の順番をミスっているが、NoError!!