node.js - Typescript 忽略 fs.d.ts 中的 MakeDirectoryOptions 定义
为什么这个不能编译?
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 Community 的 TypeScript 新手。正在将可用的 Node.JS 代码转换为 TypeScript。我在项目启动时调用一个函数。这段代码在 Node.JS 中可以工作,但在 TypeScript 中显示编译错误。
fs.mkdirSync(folder, { recursive: 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'.
它似乎正在忽略 fs.d.ts 中对 MakeDirectoryOptions 的定义:
到目前为止我所做的:
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 * as fs from 'fs';" 这一行改成:var fs=require("fs"); 一切都能编译并正常工作 - 但是我觉得这有点作弊?
它似乎正在忽略 fs.d.ts 中对 MakeDirectoryOptions 的定义:
function mkdirSync(path: PathLike, options?: number | string | MakeDirectoryOptions | null): void;
导出接口 MakeDirectoryOptions {等等...}
答案#1
我在这里找到了答案:
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34980
上面的答案对于 TypeScript 新手来说仍然很困惑,所以我全局卸载了这些类型并重新安装了最新版本。我以管理员身份打开了命令提示符,并输入如下内容。
npm uninstall -g @types/node
npm install -g @types/node@latest
这似乎已经解决了问题。如果您有其他建议,请告知。
答案#2
对我来说,当我降级 @types/node 版本时,问题就解决了
"@types/node": "16.0.0"
到
"@types/node": "15.12.5"
