错误:TS2345:使用打字稿编译时
Posted
技术标签:
【中文标题】错误:TS2345:使用打字稿编译时【英文标题】:Error: TS2345: when compiling with typescript 【发布时间】:2016-08-21 10:36:06 【问题描述】:我在 nodejs / express 项目的上下文中从 1.8.10 ts 编译器收到此错误:
错误:(17, 23) TS2345: 'typeof 类型的参数 “/Users/ME/Documents/Apps_And_Sites/Js_Apps/site/apps/directory/directory_contr ...” 不可分配给“路由器”类型的参数。属性“参数”是 缺少类型'typeof “/Users/ME/Documents/Apps_And_Sites/Js_Apps/site/apps/directory/directory_contr...”。
这里是控制器文件:
Import * as express from "express"
import * as directory_models from "./directory_models"
import * as forms from "formidable"
let directory_router = express.Router() // HERE /////////
// GET
directory_router.get("/list", function (req, res)
function callback (err, data) // function definition
res.render("directory_views.ejs", datatest: data)
directory_models.read(
`SELECT link_title
FROM
links`,
callback)
)
// POST
directory_router.route("/create")
// Display form page
.get(function (req, res)
res.render("directory_post.ejs")
)
// Process form
.post(function (req, res)
let form = new forms.IncomingForm()
form.parse(req, function (err, fields, files)
directory_models.create(fields)
)
res.render("directory_post.ejs")
)
module.exports = directory_router
这里是web应用的入口文件:
import * as express from "express"
import * as lodash from "lodash"
import * as directory from "./apps/directory/directory_controller"
// Non require variables
let app = express()
let port = process.env.PORT || 3002
import * as forms from "formidable"
app.use("/directory", directory) // ERROR HERE ////////
app.set("views", [__dirname + "/apps/directory/views/", "views"])
app.set("view engine", "ejs")
app.listen(port, function ()
console.log("Server is listening on port:" + port)
)
它不会阻止项目正常工作,但我仍然想了解并摆脱这个错误。你知道吗?
【问题讨论】:
【参考方案1】:module.exports = directory_router
你应该这样做:
export = directory_controller
【讨论】:
确实如此,解决问题,谢谢。真的迷失了所有这些出口/进口。转译的代码是一样的,所以我猜它只是在打字稿环境中不正确的语法。以上是关于错误:TS2345:使用打字稿编译时的主要内容,如果未能解决你的问题,请参考以下文章