【问题标题】:How can I rename an imported variable from `await` so that it does not shadow other variables?如何重命名从 `await` 导入的变量,使其不会影响其他变量?
【发布时间】:2019-06-02 09:48:45
【问题描述】:

给定以下代码:

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

【问题讨论】:

    标签: node.js typescript async-await lint


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多