node.js - 返回时 typescript 不读取null

标签 node.js typescript webpack

我遇到了 Typescript 无法正确读取我的返回类型的问题。 我正在定义一个返回类型为 something | 的函数空。而 typescript 所做的是只读取 something 类型,而忽略 null,当我调用函数时可以返回它。

function nullornot(): null | string { // here is says it returns null | string
    return null
}

const response = nullornot() // but here is says it returns string

Function declaring

Function call

我的 tsconfig.json 文件

{
   "extends": "./tsconfig.paths.json",
   "compilerOptions": {
     "typeRoots": ["node_modules/@types", "src/@types"],
     "allowSyntheticDefaultImports": true,
     "lib": ["ESNext"],
     "moduleResolution": "node",
     "noUnusedLocals": true,
     "noUnusedParameters": true,
     "removeComments": true,
     "sourceMap": true,
     "target": "ESNext",
     "outDir": "lib"
   },
   "files": ["src/@types/graphql.d.ts"],
   "include": ["src/**/*.ts"],
   "exclude": [
     "node_modules/**/*",
     ".serverless/**/*",
     ".webpack/**/*",
     "_warmup/**/*",
     ".vscode/**/*"
   ]
}

我的 webpack.config.js 文件

module.exports = {
  context: __dirname,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',
  resolve: {
    extensions: ['.mjs', '.json', '.ts'],
    symlinks: false,
    cacheWithContext: false,
    plugins: [
      new TsconfigPathsPlugin({
        configFile: './tsconfig.paths.json',
      }),
    ],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  optimization: {
    concatenateModules: false,
  },
  target: 'node',
  externals: [nodeExternals()],
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      {
        test: /\.(tsx?)$/,
        loader: 'ts-loader',
        exclude: [
          [
            path.resolve(__dirname, 'node_modules'),
            path.resolve(__dirname, '.serverless'),
            path.resolve(__dirname, '.webpack'),
          ],
        ],
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
        },
      },
      {
        test: /\.graphql?$/,
        use: [
          {
            loader: 'webpack-graphql-loader',
            options: {
              // validate: true,
              // schema: "./src/schema.graphql",
              // removeUnusedFragments: true
              // etc. See "Loader Options" below
            }
          }
        ]
      }
    ],
  },
  plugins: [],
};

感谢大家的帮助!

最佳答案

将结果键入为 string | null,你需要像这样在compilerOptions中启用strictNullChecks:

"compilerOptions": {
  "strictNullChecks": true
}

TypeScript docs了解更多详情。

关于node.js - 返回时 typescript 不读取null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65563333/

相关文章:

node.js - 如何在 dynamoDB 上使用分页

javascript - Angular 2 - 更好地使用 Angular-cli 还是在样板之前准备干净?

node.js - 即使我将内容类型放入 header 中,请求也会返回无效或不存在的内容类型

node.js - 保持 Node JS 应用程序运行

CSS 背景图片网址未显示

javascript - 在 Vue 中代理 webpack 开发服务器

css - Create React App 生成的应用程序的 Sass 文件中的相对 img url 问题

node.js - 我可以将 cookie 添加到 webpack 开发服务器代理吗?

node.js - 渲染页面时不添加样式

angular - 在 Angular 的 http 订阅操作中使用 ngx-spinner