【问题标题】:Typescript + react-create-app with express problemTypescript + react-create-app 有表达问题
【发布时间】:2020-08-12 03:48:35
【问题描述】:

我是 Typescript 的新手,所以我创建了一个 react-create-app 项目,我知道的问题是我想用 express 连接服务器。

问题是我创建了一个文件夹 -> src/server/server.ts

所以,react 会自动创建一个 tsconfig.json,它是:

{
 "compilerOptions": {
   "target": "es5",
   "lib": [
     "dom",
     "dom.iterable",
     "esnext"
   ],
   "allowJs": true,
   "skipLibCheck": true,
   "esModuleInterop": true,
   "allowSyntheticDefaultImports": true,
   "strict": true,
   "forceConsistentCasingInFileNames": true,
   "module": "esnext",
   "moduleResolution": "node",
   "resolveJsonModule": true,
   "isolatedModules": true,
   "noEmit": true,
   "jsx": "react"
 },
 "include": [
   "src"
 ],
 "exclude": ["src/server"]
}

我添加了排除部分并创建了其他 tsconfig 文件

tsconfig.server.json

{
  "compilerOptions": {
    /* Basic Options */                 /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */                      /* Concatenate and emit output to single file. */
    "outDir": "./build",                        /* Redirect output structure to the directory. */
    "rootDir": "./src/server",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    /* Advanced Options */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "extends": "./tsconfig.json"
}

当我在命令行中输入 tsc 时,我希望将 server.ts 编译到项目根目录的 build 文件夹中,但仍然无法正常工作。

这很简单,什么都不做。

你知道我在这里做错了什么吗?

【问题讨论】:

    标签: javascript node.js typescript express


    【解决方案1】:

    您需要使用tsc --project --build 来构建,或者如果您正在开发,我建议您使用tsc --project --watch,它会在每次文件更改时重新编译。

    编译器选项的完整列表可以在here找到

    【讨论】:

    • 感谢您的回答,在这种情况下不起作用,因为问题是它看起来不像的 tsconfig.server.json 与 tsconfig.json 连接,我这样做了,它说很好,但它不会从服务器生成我需要的东西
    • 您是否尝试使用 tsconfig.server.json 而不是 tsconfig.json?
    • 我正在尝试使用 tsconfig.json 作为基础,使用 tsconfig.server.json 作为服务器的扩展
    • 我最终找到了解决方案,我必须创建一个基础 tsc 并按照您告诉我的内容进行制作,它有效!谢谢!!
    • 我看到你是新来的,当一个答案解决了你的问题/问题时,你应该总是接受它,让其他人知道什么是有效的
    猜你喜欢
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2021-03-27
    • 2019-02-06
    • 2020-04-14
    • 1970-01-01
    • 2022-01-19
    • 2021-03-04
    相关资源
    最近更新 更多