简体   繁体   中英

Typescript complaining about type on destructuring

I have the following code:

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 };

The VSCode is complaining with the TS7031 warning:

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

How can I get rid of that warning?

TypeScript cannot infer what type the System value should be from your code (or, more specifically, it cannot infer the type of first parameter to function a ). Just add an explicit type annotation to fix this:

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

});

Replace string with the actual type of System (perhaps typeof systemType ?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM