incubator-answer icon indicating copy to clipboard operation
incubator-answer copied to clipboard

Fix RouteNode type error

Open ppchart opened this issue 3 years ago • 1 comments

image

Problem Description:

interface RouteNode extends RouteObject The original code RouteNode directly inherits RouteObject, and an error will be reported: An interface can only extend the object type or the intersection of object types using static known members. ts(2312)

Problem causes:

RouteObject is a dynamic type: IndexRouteObject | NonIndexRouteObject It is not a definite static type and therefore cannot be directly inherited

Solution:

Declare certain types for IndexRouteObject and NonIndexRouteObject respectively: IndexRouteNode and NonIndexRouteNode Re-declare the dynamic type: type RouteNode = IndexRouteNode | NonIndexRouteNode


问题描述:

interface RouteNode extends RouteObject 原代码 RouteNode 直接继承了 RouteObject,会报错:接口只能扩展使用静态已知成员的对象类型或对象类型的交集。ts(2312)

问题原因:

RouteObject 是一个动态类型:IndexRouteObject | NonIndexRouteObject 它不是一个确定的静态类型,因此不能够被直接继承

解决方案:

IndexRouteObjectNonIndexRouteObject 分别声明确定的类型:IndexRouteNodeNonIndexRouteNode 再声明动态类型:type RouteNode = IndexRouteNode | NonIndexRouteNode

ppchart avatar Oct 25 '22 03:10 ppchart

@robinv8 can we approve it or give some suggestions for it

ppchart avatar Oct 27 '22 07:10 ppchart

@ppchart Strange, I don't see a type error, I wonder if it's caused by an external cause?

I also did not find that RouteObject is a dynamic type, as shown below image

robinv8 avatar Nov 01 '22 14:11 robinv8

I found the reason: (I use npm)

  • This issue does not appear when I use pnpm to install dependencies, and the installed react-router-dom/react-router version is: v6.4.0, because we have pnpm lock file
  • This issue occurs when I use npm to install dependencies, and the react-router-dom/react-router version is: v6.4.3, because we not have package lock file

When [email protected] -> [email protected] it updates RouteObject to dynamic type

I found this relevant PR about react-router updated as follows: fix: Strengthen route typings

It's means we have three solutions:

  1. So maybe we can upload the package lock file to avoid this issue I'm having
  2. Or update react-router version and merge this PR(There may be a better solution after the upgrade, not necessarily the current PR)
  3. we can also choose to disallow the use of npm, in which case this PR can also be closed, we don't need to do anything

ppchart avatar Nov 02 '22 02:11 ppchart

@robinv8 ........ It looks like the second solution is chosen, but it requires us to upgrade react-router-dom OK, I commited new PR to update react-router-dom https://github.com/answerdev/answer/pull/59

ppchart avatar Nov 02 '22 03:11 ppchart