typescript - 未捕获的类型错误 : Cannot read property 'release' of undefined

标签 typescript redux rxjs ngrx angular5

大家好!

我需要一些帮助。 我想用特定的结构测试我的服务树。

我的测试如下:

describe(`Service selector`, () => {
  describe(`getCurrentServiceTree`, () => {
    it(`should build the generic service tree`, () => {
      const services = [
        '{http://namespace-example.fr/service/technique/version/1.0}Localpart0',
        '{http://namespace-example.fr/service/technique/version/1.0}Localpart1',
        '{http://namespace-example.fr/service/technique/version/2.0}Localpart2',
        '{http://namespace-example.fr/service/technique/version/3.0}Localpart3',
        '{http://namespace-example.fr/service/technique/version/3.0}Localpart4',
      ];

      const expectedTree: TreeElement<any>[] = [
        {
          name: 'http://namespace-example.fr/service/technique/version/1.0',
          children: [
            {
              name: 'Localpart0',
              children: [],
              isFolded: false,
              cssClass: 'item-localpart',
            },
            {
              name: 'Localpart1',
              children: [],
              isFolded: false,
              cssClass: 'item-localpart',
            },
          ],
          isFolded: false,
          cssClass: 'item-namespace',
        },
        {
          name: 'http://namespace-example.fr/service/technique/version/2.0',
          children: [
            {
              name: 'Localpart2',
              children: [],
              isFolded: false,
              cssClass: 'item-localpart',
            },
          ],
          isFolded: false,
          cssClass: 'item-namespace',
        },
        {
          name: 'http://namespace-example.fr/service/technique/version/3.0',
          children: [
            {
              name: 'Localpart3',
              children: [],
              isFolded: false,
              cssClass: 'item-localpart',
            },
            {
              name: 'Localpart4',
              children: [],
              isFolded: false,
              cssClass: 'item-localpart',
            },
          ],
          isFolded: false,
          cssClass: 'item-namespace',
        },
      ];

      const generatedStore: Partial<IStore> = {
        services: {
          byId: services.reduce(
            (acc, name, index) => ({ ...acc, [`service${index}`]: { name } }),
            {}
          ),
          allIds: services.map((_, index) => `service${index}`),
          isFetchingServices: false,
          selectedServiceId: '',
        },
      };

      expect(getCurrentServiceTree(generatedStore as any)).toEqual(
        expectedTree
      );
    });
  });
});

我不知道这是 TypeScript 问题还是与 RxJS 相关..或者只是我的代码:(

当我将鼠标放在 getCurrentServiceTree 上时,我得到了:

(alias) getCurrentServiceTree(state: IStore): TreeElement[] import getCurrentServiceTree

不知道我用得对不对Partial<T> .
我无法将 generatedStore 输入为 ..
当我将鼠标放在 上时,我看到:

[ts] L'argument de type '() => any' n'est pas attribuable au paramètre de type 'IStore'. La propriété 'ui' est manquante dans le type '() => any'.

export const getCurrentServiceTree = createSelector(
  getSelectedWorkspaceId,
  getServicesAllIds,
  getServicesById,
  (selectedWorkspaceId, servicesAllIds, servicesByIds): TreeElement<any>[] => {
    const baseUrl = `/workspaces/${selectedWorkspaceId}/services`;

    const servicesWithNspLocalpart = servicesAllIds.map(id => ({
      ...findNamespaceLocalpart(servicesByIds[id].name),
      id,
    }));

    const groupedByNamespace = groupByNamespace(servicesWithNspLocalpart);

    return groupedByNamespace.map(nspWithLocalparts => ({
      name: nspWithLocalparts.namespace,
      isFolded: false,
      cssClass: `item-namespace`,
      link: ``,
      children: nspWithLocalparts.localparts.map(localpart => ({
        name: localpart.name,
        isFolded: false,
        cssClass: `item-localpart`,
        link: `${baseUrl}/${localpart.id}`,
        children: [],
      })),
    }));
  }
);

我应该做什么?
有人可以解释一下如何解决我的问题吗?

最佳答案

我今天刚刚遇到了完全相同的错误。

一开始并不清楚错误从何而来。

我做了什么调试:

  • 而不是使用 ng test 运行测试(也许是 headless ),不要使用 headless 模式,并使用参数 --no-sourcemaps 启动它们

  • 我在浏览器中看到了一条(稍微)更好的消息,至少告诉我哪个文件导致了问题

  • 即使从那里,也不清楚问题到底出在哪里,我发现ng test不报告循环部门,但 ng serve确实

  • 从那里找到导致循环依赖的函数相当容易,我只需将其移动到另一个文件中(无论如何这更有意义)

关于typescript - 未捕获的类型错误 : Cannot read property 'release' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48154330/

相关文章:

javascript - 类型 'csrf_token' 上不存在属性 'Object'

angularjs - Angular 2 updateNg1Component 不工作 - 未知提供商

javascript - 如何将对象插入另一个根数组内的数组中

reactjs - 在 Redux 中获取初始状态

angular - rxjs observable.interval();在浏览器中抛出不是函数异常

typescript - 为什么 typescript mixins 需要一个带有单个 rest 参数 any[] 的构造函数?

typescript - 如何在 typescript 中定义对象类型的对象

javascript - 如何在没有 store.dispatch 的情况下链接异步操作并等待结果

angular - 使用 ngrx 处理组件错误

javascript - 如何使用 rxjs 有条件地缓冲?