File/Blob - eslint(无-undef)

File/Blob - eslint(no-undef)

在NextJS/TypeScript项目中创建Apollo Client时,需要判断当前操作是否为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)
}

您可以将 browser: true 添加到 env prop 内的 ESLint 配置文件中:

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