首页 > 解决方案 > 打字稿抱怨解构类型

问题描述

我有以下代码:

import { GraphQLNonNull, GraphQLString, GraphQLList, GraphQLInt } from 'graphql';

import systemType from './type';
import { resolver } from 'graphql-sequelize';

let a = ({System}) => ({
  system: {
    type: systemType,
    args: {
      id: {
        description: 'ID of system',
        type: new GraphQLNonNull(GraphQLInt)
      }
    },
    resolve: resolver(System, {
      after: (result: any[]) => (result && result.length ? result[0] : result)
    })
  },
  systems: {
    type: new GraphQLList(systemType),
    args: {
      names: {
        description: 'List option names to retrieve',
        type: new GraphQLList(GraphQLString)
      },
      limit: {
        type: GraphQLInt
      },
      order: {
        type: GraphQLString
      }
    },
    resolve: resolver(System, {
      before: (findOptions: any, { query }: any) => ({
        order: [['name', 'DESC']],
        ...findOptions
      })
    })
  }
});

export = { a: a };

VSCode 抱怨 TS7031 警告:

Binding element 'System' implicitly has an 'any' type

我怎样才能摆脱那个警告?

标签: typescriptgraphqlsequelize.js

解决方案


TypeScript 无法System从您的代码中推断出值应该是什么类型(或者,更具体地说,它无法推断出 function 的第一个参数的类型a)。只需添加显式类型注释即可解决此问题:

let a = ({System}: { System: string }) => ({

});

替换为(也许?)string的实际类型Systemtypeof systemType


推荐阅读