打字稿抱怨拆解的类型

时间:2019-05-29 21:19:54

标签: typescript graphql sequelize.js

我有以下代码:

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

如何摆脱该警告?

1 个答案:

答案 0 :(得分:1)

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

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

});

string替换为实际的System类型(也许是typeof systemType?)