You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GraphQL与NestJS Type泛型不兼容:调用createFilterClass类型匹配失败

类型错误:调用createFilterClass时参数不匹配

我定义了如下函数:

import { GraphQLScalarType } from 'graphql';
import { Type } from '@nestjs/common';

export function createFilterClass<T extends typeof GraphQLScalarType>(
  FilterType: Type<T>
) {
  // ...
}

但所有调用尝试均失败:

调用示例1

import { GraphQLString } from 'graphql';
import { createFilterClass } from './filter-class';

const c = createFilterClass(GraphQLString);

错误提示:

类型“GraphQLScalarType<string, string>”的参数不能赋给类型“Type”的参数。
类型“GraphQLScalarType<string, string>”缺少类型“Type”中的以下属性:
apply、call、bind、prototype 及另外4个属性。ts(2345)

调用示例2

const c = createFilterClass(GraphQLString);

错误提示:

类型“string”的参数不能赋给类型“Type”的参数。
类型“string”不能赋给类型“Type”。ts(2345)

注意:我必须在createFilterClass中使用Type<T>,否则以下代码会报错:

@Field(() => FilterType, { nullable: true }) // 类型“{ nullable: true; }”的参数不能赋给类型“FieldOptionsExtractor<T>”的参数。ts(2345)
myField?: T;

内容的提问来源于stack exchange,提问作者Simeon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 17:56:09