打字稿忽略了MakeDirectoryOptions

时间:2019-08-08 13:45:26

标签: node.js typescript

为什么不编译?

import * as fs from 'fs';
var folder: string = "c:/test1/test2/test3/";
fs.mkdirSync(folder, { recursive:true }); // the {object} shows an error 

我是使用MS Visual Studio 2019社区的TypeScript新手。将工作的Node.JS代码转换为TypeScript。我在项目启动时调用了一个函数。该代码在Node.JS中有效,但在TypeScript中显示编译错误。

fs.mkdirSync(文件夹,{递归:true}); // {object}无法编译。

我看不到为什么我不允许传递指定递归位的对象。目前在新手荒野中徘徊。建议将不胜感激。 当我将鼠标悬停在编译错误上时,会出现以下弹出窗口:

(property) recursive: boolean

(TS) Argument of type '{ recursive: boolean }' is not assignable to parameter of type 'string | number'. 
Type '{ recursive: boolean }' is not assignable to type 'number'.

它似乎忽略了MakeDirectoryOptions的fs.d.ts定义:

我到目前为止所做的:

1)我搜索了驱动器C :,并找到了4个fs.d.ts文件。每个都包含以下定义:

function mkdirSync(path: PathLike, options?: number | string | MakeDirectoryOptions | null): void;

export interface MakeDirectoryOptions {
        /**
         * Indicates whether parent folders should be created.
         * @default false
         */
        recursive?: boolean;
        /**
         * A file mode. If a string is passed, it is parsed as an octal integer. If not specified
         * @default 0o777.
         */
        mode?: number;
    }

2)我尝试修改package.json设置:

  "devDependencies": {
    "@types/node": "^8.0.14",
    "typescript": "^3.2.2" // forcing this to use the latest 3.6.0
  }

重现该问题的代码: ----- server.ts

import * as fs from 'fs';
//import fs = require("fs"); // does not work
//var fs = require("fs"); // compiles   

var folder:string = "c:/test1/test2/test3/";
var result: string = "untested";
try {
    // test for the existance of a folder 
    if (!fs.existsSync(folder)) {
        // create the folder if found to not exist
        fs.mkdirSync(folder, { recursive: true }); // { recursive: true } shows as compile error
        result = "created";
    }
    else {
        result = "exists already";
    }
}catch(err) {
    result = err.message;
}

import * as http from 'http';
var port: number | string = process.env.port || 1337
http.createServer(function (req, res) {

    res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Operation result: = ' + result + "\n");

}).listen(port);

----- package.json

{
  "name": "trials",
  "version": "0.0.0",
  "description": "trials",
  "main": "server.js",
  "author": {
    "name": ""
  },
  "scripts": {
    "build": "tsc --build",
    "clean": "tsc --build --clean"
  },
  "devDependencies": {
    "@types/node": "^8.0.14",
    "typescript": "^3.2.2"
  }
}

----- tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "lib": ["es6"],
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

-----代码结尾

如果我将“ import *从'fs'更改为fs;”行到: var fs = require(“ fs”); 一切都可以编译和运行-但我认为这是作弊吗?

它似乎忽略了MakeDirectoryOptions的fs.d.ts定义:

function mkdirSync(path:PathLike,options ?: number | string | MakeDirectoryOptions | null):void;

导出接口MakeDirectoryOptions {etc ....}

2 个答案:

答案 0 :(得分:1)

我在这里找到了答案:

https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34980

对于TypeScript新手来说,上面的答案仍然令人困惑,因此我对类型进行了全局卸载并重新安装了最新版本。我以管理员身份打开命令提示符,然后键入以下内容。

npm uninstall -g @types/node 
npm install -g @types/node@latest

这似乎已经解决了问题。如果您不建议,请告知。

答案 1 :(得分:0)

对我来说,当我降级@types/node 版本时问题就解决了

    "@types/node": "16.0.0"

    "@types/node": "15.12.5"