【问题标题】:Typescript complaining about type on destructuring打字稿抱怨解构类型
【发布时间】:2019-05-30 05:56:26
【问题描述】:

我有以下代码:

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

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

【问题讨论】:

    标签: typescript graphql sequelize.js


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 2016-11-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2021-06-26
      • 2018-01-05
      相关资源
      最近更新 更多