gpt4 book ai didi

javascript - typescript :找不到 namespace

转载 作者:行者123 更新时间:2023-11-30 14:00:47 24 4
gpt4 key购买 nike

我有以下简单的 NodeJS 代码:

const express = require('express');

const server: express.Application = express();

我正在将 Typescript 添加到我的项目中并且是新手,所以请原谅我。使用上面的代码我得到以下问题/错误:

对于要求:

var require: NodeRequire (id: string) => any (+1 overload)
'require' call may be converted to an import.

对于 express.Application 用法:

Cannot find namespace 'express'.

如果我将“require”切换为“import”,它会修复命名空间错误,但不再是有效的 Node 代码,因此不会运行(抛出有关导入的意外 token 的新错误)。

使用 Typescript 编写这样的 Node 代码以避免这些错误的正确方法是什么?

我的 tsconfig.json 看起来像这样:

{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "esnext",
},
"exclude": ["node_modules"],
}

最佳答案

如果你是 React 开发人员,并且像我一样结束了这里 - 如果文件中有 JSX 语法,请尝试将文件的扩展名从 .ts 更改为 .tsx

关于javascript - typescript :找不到 namespace ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56341127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com