【发布时间】:2022-07-04 08:18:51
【问题描述】:
我正在尝试编写一个函数来添加一个新的 Google Secret Manager 版本,然后销毁之前的旧版本。
我可以轻松添加新版本,但要销毁旧版本,我需要它的版本号。
根据these docs,我尝试通过const [version] = await secrets.addSecretVersion() 获取新的秘密版本号,然后从中减去1。
但 TypeScript 抱怨 version 不是数字:
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)
这是我添加新版本和删除旧版本的代码:
const addSecretVersion = async (secretName: string, value: string) => {
const parent = `projects/my-project/secrets/${secretName}`;
const payload = Buffer.from(value, 'utf8');
// Add the new secret
const [version] = await secrets.addSecretVersion({
parent: parent,
payload: {
data: payload,
},
});
const oldVersionNumber = version - 1; //<--- TypeScript error here
// Destroy the old secret (to avoid billing)
const oldSecret = `projects/my-project/secrets/${secretName}/versions/${oldVersionNumber}`;
await secrets.destroySecretVersion({
name: oldSecret,
});
};
【问题讨论】: