【问题标题】:Uncaught TypeError: Cannot read property 'release' of undefined未捕获的类型错误:无法读取未定义的属性“释放”
【发布时间】:2018-06-17 16:04:51
【问题描述】:

大家好!

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

我的测试看起来像:

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 上时,我得到了:

(别名)getCurrentServiceTree(状态:IStore):TreeElement[] 导入getCurrentServiceTree

我不知道我是否正确使用Partial&lt;T&gt;
我无法将 generatedStore 键入为 ..
当我把鼠标放在上面时,我就有了:

[ts] L'argument de type '() => any' n'est pas attribuable au 参数类型“IStore”。 La propriété 'ui' est manquante dans le 输入“()=>任何”。

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: [],
      })),
    }));
  }
);

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

【问题讨论】:

  • 也许尝试用const expectedTree: any替换const expectedTree: TreeElement&lt;any&gt;[]
  • @RichardMatsen 不起作用,因为我有子元素:(
  • 如果你能翻译错误会有所帮助。
  • `() => any' 类型的参数不能分配给 'IStore' 类型的参数。缺少属性“ui”?从类型“()=>任何”。 (用英文看够了)。
  • @christophechevalier - mais oui,'any[]' 怎么样?

标签: typescript redux rxjs ngrx angular5


【解决方案1】:

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

一开始并不清楚错误是从哪里来的。

我做了什么调试:

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

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

  • 即使从那里,问题的确切位置也不是很明显,我发现 ng test 没有报告循环部门,但 ng serve 报告了

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

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 2017-06-08
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 2019-02-26
    • 2021-12-25
    相关资源
    最近更新 更多