代码之家  ›  专栏  ›  技术社区  ›  left click

如何声明一个模块来进行命名导入?

  •  0
  • left click  · 技术社区  · 6 年前

    我在用 graphql-tag . 我的档案就是这样。

    ./操作.graphql

    Query User {
       ...
    }
    

    import { User } from './operation.graphql'; /// Module ''*.graphql'' has no exported member 'User'.
    

    /索引d.ts

    declare module '*.graphql' {
        import { DocumentNode } from 'graphql';
    
        const value:DocumentNode;
    
        export default value;
    }
    

    一个应用程序可以很好地工作,但我想防止这个错误。

    当我执行默认导入时,它工作得很好,但是正如您所看到的,我在命名导入时遇到了一个错误。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Matt McCutchen    6 年前

    baseUrl paths 因此,您可以使用非相对导入,或者将 d.ts 每个GraphQL文件旁边的文件,而不是使用 declare module ,我不确定):

    declare module 'operation.graphql' {
        import { DocumentNode } from 'graphql';
    
        export const User: DocumentNode;
    }
    

    或者您可以放弃,让所有的GraphQL文件都是类型 any :

    declare module '*.graphql';