2
関数の入力パラメータの1つに基づいて、 "selected"のいずれかの型の識別された共用体を返すにはどうすればよいですか?TypeScript: "one option selected"を指定して、識別された共用体を返します。
type KeyValueDocument = {
key: "type-a";
propA: string;
} | {
key: "type-b";
propB: string;
}
function getKeyValue(key: string): KeyValueDocument {
// ... implementation ...
assert(result.key == key);
return result;
}
const value = getKeyValue("type-b");
console.log(value.propB); // Bang!