如何让 tsconfig 在将它们导入组件时识别“.graphql”文件、别名?

How to get tsconfig to recognize ".graphql" files, aliases, when importing them into components?

因此,我将一些“graphql”文件包含到我的组件中,但它们给我带来了 TS 错误,因为无法识别导入路径。一切正常,只是给出了 TS 错误。

import QUERY_GET_CATS from "@gql/GetCats.graphql";

我得到的错误: TS2307:找不到模块“@gql/GetCat.graphql”或其对应的类型声明

这是我的 tsconfig:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "*": [
        "./*"
      ],
      "@gql/*": [
        "./api/gql/*"
      ]
    },
    "lib": ["dom", "dom.iterable", "esnext"],
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "isolatedModules": true,
    "jsx": "preserve",
    "downlevelIteration": true,
    "allowJs": true,
    "skipLibCheck": true,
    "resolveJsonModule": true
  },
  "exclude": [
    "cypress",
    "coverage",
    "node_modules",
    "build",
    "dist",
    "out",
    ".next",
    "next.config.js",
    "./**/*.js",
    "./**/*.jsx",
    "./**/*.scss"
  ],
  "include": [
    "next-env.d.ts",
    "./**/*.ts",
    "./**/*.tsx"
  ]
}

我的 eslintrc

const path = require('path');

module.exports = {
  parserOptions: {
    ecmaVersion: 2019,
    sourceType: 'module',
    project: './tsconfig.json',
    ecmaFeatures: {
      jsx: true,
    },
  },
  parser: '@typescript-eslint/parser',
  extends: [
    'eslint:recommended',
    'plugin:react-hooks/recommended',
    'plugin:react/recommended',
    'airbnb-typescript',
    'prettier',
    'prettier/react',
    'eslint-config-prettier',
    'eslint-config-prettier/@typescript-eslint',
    'plugin:cypress/recommended'
  ],
  globals: {
    cy: true
  },
  rules: {
    strict: ['error', 'never'],
    'import/no-cycle': 0,
    'import/prefer-default-export': 0,
    'global-require': 0,
    'react/jsx-key': 1,
    'prefer-destructuring': 1,
    "import/no-extraneous-dependencies": 1,
    'no-console': 'warn',
    'no-param-reassign': [
      2,
      {
        props: false,
      },
    ],
    'react/prop-types': 0,
    'camelcase': ['warn', {ignoreDestructuring: true, properties: 'never'}],
    'react/no-unescaped-entities': 0,
    'import/extensions': 'off',
    'react/jsx-props-no-spreading': ['warn', {custom: 'ignore'}],
    'jsx-a11y/click-events-have-key-events': 0,
    'jsx-a11y/no-static-element-interactions': 0,
  },
  env: {
    browser: true,
    jest: true,
    es2020: true,
    'cypress/globals': true,
  },
  plugins: ['eslint-plugin-cypress'],
  overrides: [
    {
      settings: {
        'import/resolver': {
          jest: {
            jestConfigFile: path.join(__dirname, '/jest.config.js'),
          },
        },
      },
    },
  ],
};

注意:当我将“./**/*.graphql”添加到“include”数组时,没有帮助。 注意:我的其他别名有效。只是不是“graphql”。

因为这些文件是 gql 语法,而不是 Typescript 词典的一部分,您需要在类型定义文件中将它们定义为 ambient 模块 in the typescript docs.

documentNode.d.ts

declare module '*.graphql' {
  import {DocumentNode} from 'graphql';

  const value: DocumentNode;
  export = value;
}

您还需要将 "./**/*.graphql" 添加到 tsconfig 的 include 部分。

应该注意的是,这基本上是一个 dummy/placeholder,它允许您将文件导入代码库,但不会从内容中获取任何类型信息。 graphql code generator 的人提供了一个名为 TypedDocumentNode 的包,它可以让你将完全类型化的 DocumentNodes 拉到你的项目中,但我还没有尝试过所以 YMMV。