gpt4 book ai didi

node.js - 如何在 typescript 中导入 recursive-readdir

转载 作者:行者123 更新时间:2023-12-04 08:23:16 30 4
gpt4 key购买 nike

我在导入和使用 npm 模块 recursive-readdir 方面有点挣扎在 typescript 程序中。
之后 npm install recursive-readdir @types/recursive-readdir (@types/recursive-readdir@2.2.0, recursive-readdir@2.2.2) 以下代码段有效:

import * as RecursiveReaddir from 'recursive-readdir';

async function test () {
const files = await RecursiveReaddir.default('/tmp');
...
}
文件 node_modules/@types/recursive-readdir/index.d.ts包含:
...
import * as fs from "fs";
declare namespace RecursiveReaddir {
type IgnoreFunction = (file: string, stats: fs.Stats) => boolean;
type Ignores = ReadonlyArray<string|IgnoreFunction>;
type Callback = (error: Error, files: string[]) => void;
interface readDir {
(path: string, ignores?: Ignores): Promise<string[]>;
(path: string, callback: Callback): void;
(path: string, ignores: Ignores, callback: Callback): void;
}
}

declare var recursiveReadDir: RecursiveReaddir.readDir;
export = recursiveReadDir;
我的问题:
  • 函数RecursiveReaddir.default(...)在哪里来自(哪里?
  • 是否有更易读和更直观的导入和使用?
    例如 ìmport { readDir } from 'recursive-readdir';或类似的东西?

  • 测试程序可在以下位置获得:
    https://github.com/ManfredSteiner/stackovf_65391266

    最佳答案

    我在你的代码中得到了错误。(node:7541) UnhandledPromiseRejectionWarning: TypeError: RecursiveReaddir.default is not a function所以,我通过引用现有的测试代码来实现它。见下文

  • npm/recursive-readdir
  • github/recursive-readdir

  • 测试.ts
    // ./test.ts
    import recursive from 'recursive-readdir';

    async function test () {
    const files = await recursive(__dirname);
    console.log(files);
    }

    test()
    命令
    # node v12.13.1
    # npm v6.11.3
    npm install @types/recursive-readdir && \
    npm install recursive-readdir

    + @types/recursive-readdir@2.2.0
    + recursive-readdir@2.2.2

    tsc && node ./build/test.js
    结果
    # Success
    [ '/Users/kmk/Project/src/github.com/myeongkil/tmp/stackoverflow/recursive-readdir/build/test.js' ]

    关于node.js - 如何在 typescript 中导入 recursive-readdir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65391266/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com