我正在学习TypeScript,目前正在尝试koa,我认为我已经理解了函数定义,并且tsc同意,因为它可以很好地编译。另一方面,如果我尝试运行ts-node,它将失败,并显示以下错误。
const sequelize = new Sequelize(connectionURI(env));
^
TypeError: connectionURI is not a function
我尝试更改导出和导入无济于事,甚至尝试操作tsconfig,但这几乎就像ts-node忽略了它一样。这些是我当前的设置
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"noImplicitAny": true,
"declaration": true,
"outDir": "./lib",
"strict": true,
"sourceMap": true,
"resolveJsonModule": true,
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/__tests__/*",".vscode"]
}
这是我尝试导入的模块
let connectionURI : (env: string) => string;
connectionURI = (env: string):string => {
switch (env) {
case 'development': {
return 'uri';
}
default: {
return ' ';
}
}
}
export = connectionURI
这是我的服务器。
import { Sequelize } from 'sequelize';
import * as app from './app';
import * as connectionURI from './conf/conf';
const port = process.env.PORT || 3000;
const env = process.env.NODE_ENV || 'development';
const sequelize = new Sequelize(connectionURI(env));
const server = app.listen(port);
console.info(`Listening to http://localhost:${port} `);
将导出更改为
export default {connectionURI}
和导入到
import connectionURI from './conf/conf';
我可以在一件事上使tsc和ts-node协调一致
Cannot invoke an expression whose type lacks a call signature. Type '{ connectionURI: (env: string) => string; }' has no compatible call signatures.
但是我想在功能足够之前就声明它,这是我做错了吗?