Nodejs打字稿应用程序中的“预期变量声明”

时间:2019-07-17 18:51:57

标签: node.js typescript

创建了一种删除目录中文件的方法。

const delete: (dirName: string, fileName: string, callback: (error: string | boolean) => void) => void = (dirName, fileName, callback) => {
  fs.unlink(baseDataDir + dirName + "/" + fileName + ".json", (error) => {
    if (!error) {
      callback(false);
    } else {
      callback(error);
    }
  });
};

const fsHandler: IFSHandler = {
  create,
  read,
  update,
  delete
};

module.exports = fsHandler;

fsHandler定义的接口:

export interface IFSHandler {
  create: (dirName: string, fileName: string, data: any, callback: (error: string | boolean) => void) => void;
  read: (dirName: string, fileName: string, callback: (error: string | boolean, data: any) => void) => void;
  update: (dirName: string, fileName: string, data: any, callback: (error: string | boolean, fileDesc: any) => void) => void;
  delete: (dirName: string, fileName: string, callback: (error: string | boolean) => void) => void;
}

打字稿错误:

yarn run v1.13.0
$ tsc && node dist/index.js
src/services/fsHandler.ts:69:7 - error TS1134: Variable declaration expected.

69 const delete: (dirName: string, fileName: string, callback: (error: string | boolean) => void) => void = (dirName, fileName, callback) => {
         ~~~~~~

src/services/fsHandler.ts:69:13 - error TS1109: Expression expected.

69 const delete: (dirName: string, fileName: string, callback: (error: string | boolean) => void) => void = (dirName, fileName, callback) => {
               ~

src/services/fsHandler.ts:69:104 - error TS1109: Expression expected.

69 const delete: (dirName: string, fileName: string, callback: (error: string | boolean) => void) => void = (dirName, fileName, callback) => {
                                                                                                          ~

src/services/fsHandler.ts:84:1 - error TS1005: ':' expected.

84 };
   ~


Found 4 errors.

error Command failed with exit code 2.

1 个答案:

答案 0 :(得分:2)

delete是保留关键字,您不能使用delete作为变量名。将其更改为remove之类的东西,它将起作用。

const remove: (dirName: string, fileName: string, callback: (error: string | boolean) => void) => void = (dirName, fileName, callback) => {
  fs.unlink(baseDataDir + dirName + "/" + fileName + ".json", (error) => {
    if (!error) {
      callback(false);
    } else {
      callback(error);
    }
  });
};