DynamoDBをTypescriptから呼び出していて,起こったエラー
const result = await DBAccess.getItem(params);
const status:string = result.status.S;
ここで,左辺のstringと,右辺のstring | undefinedで型の不一致が起きているのがひとつ
さらに,resultがundefinedになる可能性があるというエラーが起きる.
以下のように解決した.
resultがundefinedになる可能性をあらかじめ排除しておき,そのうえで型アサーションをする.
const result = await DBAccess.getItem(params);
if (result === undefined) {
// do something
}
const status:string = (result!.status.S as string)
もっと良い方法があればコメントでおしえてください!