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

时间:2019-01-07 17:47:47

标签: node.js typescript async-await lint

给出以下代码:

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);

我的打字稿短绒失败,并显示:Property 'filebuffer' does not exist on type '{ bytesRead: number; buffer: Buffer; }

我不喜欢简单的buffer名称;有什么办法可以像这样通过await导入更改导出?

是添加附加行的唯一方法:let fileBuffer = buffer

1 个答案:

答案 0 :(得分:4)

您应该这样使用:而不是as

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