#Link [TypeScript 4.6で起こるタグ付きユニオンのさらなる進化](https://zenn.dev/uhyo/articles/ts-4-6-destructing-unions) - [[🗃️判別可能なユニオン(TypeScript)]]の存在をちゃんと把握できていなかった。 - [[🗃️TypeScript]] v4.6で進化した例 - ただしこれを分割代入をわけて、別々にすると絞り込まれなくなってしまう。 ```ts type OkResult<T> = { type: "ok"; payload: T; }; type ErrorResult = { type: "error"; payload: Error; }; type Result<T> = OkResult<T> | ErrorResult; function unwrapResult<T>(result: Result<T>): T { // payload はここでは T | Error 型 // 同時に分割代入することが大事 const { type, payload } = result; if (type === "ok") { // payload はこの中では T 型 return payload; } else { throw payload; } } ``` - [[🗃️TypeScript]] v4.6でも判別できない例 ```ts type OkResult<T> = { type: "ok"; value: T; }; type ErrorResult = { type: "error"; error: Error; }; type Result<T> = OkResult<T> | ErrorResult; function unwrapResult<T>(result: Result<T>): T { const { type, value, error } = result; if (type === "ok") { return value; } else { throw error; } } // error TS2339: Property 'value' does not exist on type 'Result<T>'. 12 const { type, value, error } = result; ~~~~~ error TS2339: Property 'error' does not exist on type 'Result<T>'. 12 const { type, value, error } = result; ```