简体   繁体   English

使用 TypeScript 在 Apollo 服务器中配置 REST 数据源时,对“任何”类型成员的不安全访问

[英]Unsafe access to member with 'any' type when configuring REST data source in Apollo server with TypeScript

I am trying to configure a data source in Apollo server with TypeScript:我正在尝试使用 TypeScript 在 Apollo 服务器中配置数据源:

my-rest-datasource.ts我的休息-datasource.ts

import { RESTDataSource } from 'apollo-datasource-rest';
import ResolverCtx from 'types/resolver-ctx';
import {
    IdType,
    GetResultType,
} from '../types/myTypes';

class MyRestDataSource extends RESTDataSource<ResolverCtx> {
    constructor() {
        super();
        this.baseURL = 'http://localhost:8080/';
    }

    async myGetMethod(id: IdType): Promise<GetResultType> {
        return this.get(`/get-data/${id}`);
    }
}

export default MyRestDataSource;

myResolvers.ts myResolvers.ts

import { Resolvers } from 'types/codegen';
import ResolverCtx from 'types/resolver-ctx';

const myResolvers: Resolvers<ResolverCtx> = {
    Query: {
        getData: async (_, { id }, ctx) => await ctx.dataSources.myRestDataSource.myGetMethod(id)
    },
};

export default myResolvers;

resolver-ctx.ts解析器-ctx.ts

import MyRestDataSource from '../datasources/my-rest-datasource';

type ApplicationContext = Record<string, any>;

type ApplicationDataSources = {
    myRestDataSource: MyRestDataSource;
};

type ResolverCtx = ApplicationContext & {
    dataSources: ApplicationDataSources;
};

export default ResolverCtx;

However in my-resolvers.ts I am getting ts-lint errors on the getData method:但是,在 my-resolvers.ts 中,我在 getData 方法上遇到了 ts-lint 错误:

Unsafe member access .dataSources on an `any` value.eslint@typescript-eslint/no-unsafe-member-access
Unsafe call of an `any` typed value.eslint@typescript-eslint/no-unsafe-call

If I mouse over the ctx, dataSources or myGetMethod in the return statement, the correct types are shown.如果我将鼠标悬停在 return 语句中的 ctx、dataSources 或 myGetMethod 上,则会显示正确的类型。 So I don't understand where the error is coming from.所以我不明白错误来自哪里。

This turned out to be to do with the ESLint config within the context of a mono repo.事实证明,这与单声道存储库上下文中的 ESLint 配置有关。

The code above was in a Lerna implementation with an .eslintrc file at the root which had this property in it:上面的代码在 Lerna 实现中,根目录下有一个 .eslintrc 文件,其中包含此属性:

{
    "parserOptions": {
        "project": "./tsconfig.json"
    }
}

The code above sat in a packages directory of the monorepo in a package, this did not have it's own .eslintrc上面的代码位于一个包中的 monorepo 的包目录中,它没有自己的 .eslintrc

This appeared to cause issues with the module resolution or type resolution for the ESlint Typescript linting rules, causing erroneous errors to be thrown.这似乎会导致 ESlint Typescript linting 规则的模块解析或类型解析出现问题,从而导致抛出错误错误。

The solution was to add another .eslintrc file in the package directory which was the root folder of the package containing the code in my initial question.解决方案是在包目录中添加另一个 .eslintrc 文件,该文件是包含我最初问题中的代码的包的根文件夹。 I had to add this rule to the new .eslintrc:我不得不将此规则添加到新的 .eslintrc 中:

{
    "parserOptions": {
        "project": "./packages/myPackage1/tsconfig.json"
    }
}

This seems to have fixed the resolution problem.这似乎解决了分辨率问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 收到此打字稿错误:对任何值不安全的成员访问 [key] - Getting this typescript error: Unsafe member access [key] on an any value 任何值上的 eslint 错误不安全成员访问 ['content-type'] - eslint error Unsafe member access ['content-type'] on an any value 不安全的成员访问<method>在 `any` 值上。 `this` 在编译器项目的 TypeScript 中被键入为 `any`。 如何为 TypeScript 构建 mixin?</method> - Unsafe member access <method> on an `any` value. `this` is typed as `any` in TypeScript for compiler project. How to architect mixins for TypeScript? TypeScript 不安全地使用“任何”类型的表达式错误 - TypeScript Unsafe use of expression of type 'any' ERROR 如何使用 cloneElement 修复“不安全成员访问”反应 typescript 错误? - How to fix 'unsafe-member-access' react typescript error with cloneElement? Vue 3 @typescript-eslint/no-unsafe-member-access - Vue 3 @typescript-eslint/no-unsafe-member-access 当类型是地图的任何成员时提高打字稿速度? - Improve Typescript speed when a type is any member of a map? Typescript - eslint 错误分配给类型的“任何”类型的不安全参数 - Typescript - eslint error Unsafe argument of type 'any' assigned to a type 使用 fetch api 对“任何”值的不安全分配/成员访问 - Unsafe assignment/member access of an `any` value with fetch api 对 Typescript function 中的 return 语句不安全地使用“any”类型的表达式 - Unsafe use of expression of type 'any' for return statements in Typescript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM