【问题标题】:How to include a non namepsaced module TS definition in my custom types如何在我的自定义类型中包含非命名模块 TS 定义
【发布时间】:2021-02-13 23:30:48
【问题描述】:

在我的自定义类型中,我希望使用来自graphql 模块的GraphQLSchema。如果我只是写:

interface MyThing {
  schema: GraphQLSchema
}

它没有引用模块中的GraphQLSchema 定义(它只是any)。 VSCode 然后建议自动添加导入文件:

import { GraphQLSchema } from 'graphql'

定义现在是正确的(即在 VSCode 中将鼠标悬停在其上方会弹出正确的类型)但现在我的自定义类型文件不再有效 - 即我无法在我的代码中使用 MyThing - 它不再被定义。

我的 tsconfig 确实包含我的自定义类型以及 node_modules/@types - vscode 似乎看到了它,所以我认为它没问题。我确实尝试专门添加包含定义但没有执行的 graphql 文件的路径。

我可以在没有导入的情况下使用其他命名空间的定义,但不是那个。

建议?

【问题讨论】:

标签: typescript visual-studio-code


【解决方案1】:

好的 - 对体验 TS 来说可能很明显,但对我来说不是。如果有导入语句,则必须显式导出所有内容。我的文件中没有导出语句,因为 TS 似乎不在乎,但我猜你添加导入的那一刻,所有的都必须导出。

有效(但无法导入)

interface MyThing {
  schema: any
}

不工作

import { GraphQLSchema } from 'graphql'
interface MyThing {
  schema: GraphQLSchema
}

适用于进口

import { GraphQLSchema } from 'graphql'
export interface MyThing {
  schema: GraphQLSchema
}

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2015-10-06
    • 2011-06-19
    相关资源
    最近更新 更多