}; type PetID = string & { Pet: never; }; function useUserID(userID: UserID) {} declare var petID: PetID; useUserID(petID); // Property 'User' is missing in type 'PetID' ↑実際には存在しないが、型定義の上では存在するようなプロパティを定義する例 7
"User"; }; type PetID = string & { $tag?: "Pet"; }; function useUserID(userID: UserID) {} declare var petID: PetID; useUserID(petID); // Type '"Pet"' is not assignable to type '"User"'. 12
string ? true : false; type B = A<"aaa">; // true type C = A<0x0>; // false // infer type D<T> = T extends { value: infer I } ? I : never; type E = D<"aaa">; // never type F = D<{ value: string }>; // string 14
"User" }; type GenericID<T> = string & T extends { $tag?: infer TAG } ? { $tag?: TAG; } : {}; type UserID = GenericID<User>; // string & { $tag?: "User" } // テスト type Pet = { ... } & { $tag?: "Pet" }; type PetID = GenericID<Pet>; function useUserID(userID: UserID) {} declare var petID: PetID; useUserID(petID); // Type '"Pet"' is not assignable to type '"User"'. $tag は値として存在しなくても良いので User を自然に生成できる。 15
string } & TagRecord<"User"> & NestedTag0<Domain>; type Server = {} & TagRecord<"Server">; type Client = {} & TagRecord<"Client">; function useServerUser(user: User<Server>) {} declare var clientUser: User<Client>; useServerUser(clientUser); // ↑ Type '"Client"' is not assignable to type '"Server"'. 17
Mapped TypeのTuple Type拡張を組み合わせて、型パラメータを計算する。 // Mapped Type のTuple Type 拡張 type A = ["zero", "one", "two"]; type B<T extends string[]> = { [P in keyof T]: Uppercase<T[P]>; }; type C = B<A>; // ["ZERO", "ONE", "TWO"]; 22
vertical } public struct FencePoint: Codable { public var x: Int public var y: Int public var orientation: FenceOrientation } public struct Board: Codable { ... public var fences: [FencePoint] } public class QuoridorGame { private var state: ... public init() {} public func putFence(position: FencePoint) throws { ... } public func currentBoard() -> Board { ... } } → クラスをTypsScriptに持ち出せるように なった const game = new QuoridorGame(); game.putFence({ x: 1, y: 4, orientation: "horizontal" }); const board = game.currentBoard(); board.fences.map(...); https://github.com/sidepelican/WasmCallableKit 30