如何重命名从 `await` 导入的变量,使其不影响其他变量?

How can I rename an imported variable from `await` so that it does not shadow other variables?

给定以下代码:

import * as fs from 'fs';
import {promises as fsPromises} from 'fs';

// ...

// Read the file with no encoding for raw buffer access.
const { bytesRead, buffer as fileBuffer } = await fsPromises.read(fileDescriptor, allocBuffer, 0, bytes.length, 0);

我的 Typescript linter 失败并显示:Property 'filebuffer' does not exist on type '{ bytesRead: number; buffer: Buffer; }

我不喜欢普通的 buffer 名字;有什么办法可以用这样的 await 导入更改导出?

唯一的方法是添加额外的行:let fileBuffer = buffer?

您应该使用 : 而不是 as

const { bytesRead, buffer: fileBuffer } = await fsPromises.read(handle, allocBuffer, 0, bytes.length, 0);