[英]Unexpected token ':' on an node api service when making a function strongly typed
我有一個用打字稿編寫的節點 api 服務,下面是我的示例代碼
class dataStreamConfig {
constructor() { }
conclaveObj = (firstParam: string, secondParam: number, thirdParam: any): any => {
//my business logic goes here
//return Obj
};
}
module.exports = dataStreamConfig ;
我收到以下錯誤
SyntaxError: Unexpected token ':'
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
如果我從函數中刪除類型(字符串、數字、任何),那么它工作正常。 我試圖重做 npm install。
我也做了 npm install babel-cli babel-preset-es2015。
更新
下面是我的 tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true
}
}
我還全局安裝了打字稿。 我在根目錄下使用 tsc 命令編譯項目,我也嘗試在具有該類的目錄上運行 tsc。
非常感謝在這個問題上的任何幫助/建議。
嘗試擺脫箭頭函數並使用使用 function 關鍵字編寫函數的正常方式.. 請注意此函數中的最后一個 any 表示此函數將返回的類型,這是可選的。 打字稿本身也可以檢測到這一點。 所以你是否提供它並不重要......
class dataStreamConfig {
constructor() { }
let conclaveObj = function (firstParam: string, secondParam: number, thirdParam: any): any {
//my business logic goes here
//return Obj
};
}
module.exports = dataStreamConfig ;
如需更多幫助,您可以訪問他們的文檔... https://www.typescriptlang.org/docs/handbook/functions.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.