编译时正常,将typescript中命名空间中的类与webpack一起使用时出现运行时错误
我使用编译时正常,将typescript中命名空间中的类与webpack一起使用时出现运行时错误,typescript,webpack,swagger-codegen,Typescript,Webpack,Swagger Codegen,我使用-l typescript选项生成REST消费者服务库。生成的代码如下所示(DefaultApi.ts): tsconfig.json: { "compilerOptions": { "baseUrl": "src/app", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false,
-l typescript
选项生成REST消费者服务库。生成的代码如下所示(DefaultApi.ts
):
tsconfig.json:
{
"compilerOptions": {
"baseUrl": "src/app",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"module": "commonjs"
},
"compileOnSave": false,
"include": [
"src/**/*.ts"
],
"exclude": [
"!typings/**",
"!node_modules/**"
]
}
当前版本的swagger codegen TypeScript Angular generator未将DefaultApi封装在命名空间中
更新和重新生成。如果您有任何问题,请告诉我 您有两种选择来解决此问题,一种是轻松,另一种是复杂:
export = API.Client;
现在,您可以在模块中使用import
,而不会出现问题,例如:
import {DefaultApi} from './generated-code';
api.js
文件,并在tsconfig.generate.json
中引用它。是的,这是一个js文件,萨尔萨就是在这里开始行动的。要做到这一点,您必须在tsconfig中启用allowJs
功能/// <reference path="./namespacing-code.ts" />
// typescript compiler don't warning this because is Salsa!
module.exports = API.Client;
现在,您可以使用您的生成代码而无需触摸它:从“api”导入api代码>
如果有任何疑问,这里使用这种方法进行回购。
我希望我已经帮了你你能分享你的网页配置和tsconfig吗?@JaganathanBantheswaran补充道。谢谢你看!在我看来,您使用的是typescript 2.x。您对Salsa和webpack resolve.alias解决方案的看法如下所述?嗯。。。我刚刚检查过,我使用的是2.2.1版,它似乎是最新版本()。好的。我错了。我现在看到这是angular 1.x typescript生成器,而不是angular 2。我建议与swagger codegen团队讨论一个问题,建议使用命令行选项来禁止添加名称空间错误。谢谢你的调查。
import {DefaultApi} from './generated-code';
{
"compilerOptions": {
"outFile": "module-generated-code.js"
},
"files": ["generated-code.ts"]
}
{
"compilerOptions": {
},
"exclude": ["generated-code.ts"]
}
{
"compilerOptions": {
"outFile": "module-generate-code.js", "allowJs": true
},
"files": ["generated-code.ts", "api.js"]
}
/// <reference path="./namespacing-code.ts" />
// typescript compiler don't warning this because is Salsa!
module.exports = API.Client;
{ //webpack.config
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'],
alias:{ 'api':'./module-generated-code.js'
}
}
{ //tsconfig.json
"compilerOptions": {
"allowJs": true, //remember of enabling Salsa here too
"baseUrl": ".",
"paths": {
"api":["api.js"] //it is just get type definitions from generated files
}
},