首页 > 解决方案 > 文件/Blob - eslint(no-undef)

问题描述

在 NextJS/TypeScript 项目中创建 Apollo 客户端时,我需要确定当前操作是否为 Upload,但 ESLint 抱怨FileBlob没有定义。

我可以禁用警告:// eslint-disable-next-line no-undef但我想了解为什么会有这样的警告,如果可能的话,我想在不忽略的情况下修复它。

const isFile = (value: any): boolean => {
  if (isPlainObject(value) || Array.isArray(value)) {
    return Object.values(value).map(isFile).includes(true)
  }
  const isfile = typeof File !== 'undefined' && value instanceof File
  const isblob = typeof Blob !== 'undefined' && value instanceof Blob
  return isfile || isblob
}
const isUpload = ({ variables }: any) => {
  return Object.values(variables).some(isFile)
}

在此处输入图像描述

标签: reactjstypescripteslintapollonext.js

解决方案


你可以在 props 中添加browser: true到你的 ESLint 配置文件中env

// .eslintrc
{
  "env": {
    "browser": true
  }
}

推荐阅读