我以前见过有人回答这个问题,但他们似乎没有涵盖这个特定的使用情形(或者他们不起作用/没有帮助)
import {Route} from 'vue-router';
export const detailRoute = {
path: '/detail/:id',
component: Detail,
props: (route: Route) => ({
state: route.query.state
})
};
detailRoute使用Route,我正在导入它,但我猜作为一个命名的导入{Route}它不起作用?有没有其他/更好的方法来完成这个工作?我也尝试了export {Route};
,但没有帮助。
tsconfig.json:
{
"compilerOptions": {
"target": "ES2017",
"module": "ES2015",
"moduleResolution": "Node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"noEmitHelpers": true,
"importHelpers": true,
"pretty": true,
"alwaysStrict": true,
"declaration": true,
"declarationDir": "./types",
"lib": [
"DOM",
"ES2017",
"DOM.Iterable",
"ScriptHost"
],
"baseUrl": "./client",
"paths": {
"styles/*": ["./app/core/styles/*"],
"core/*": ["./app/core/*"],
"components/*": ["./app/components/*"],
"containers/*": ["./app/containers/*"],
"assets/*": ["./assets/*"],
"config/*": ["./config/*"]
}
}
}
精确错误:TS4023: Exported variable 'detailRoute' has or is using name 'Route' from external module "/Users/chris/<projectname>/node_modules/vue-router/types/router" but cannot be named.
7条答案
按热度按时间os8fio9y1#
编译器无法计算出
detailRoute
的确切形状,因为它不知道Route
的形状。选项1
解决这个问题的一种方法是从源代码导入
Route
,从而提供编译器确定detailRoute
的形状所需的信息。由于the
index.d.ts
file invue-router
(您在问题中导入的)重新导出Route
,因此它不提供编译器所需的对Route
的 * 直接 * 引用。选项2
另一个选择是完全不使用
detailRoute
静态类型。由于
any
选择不使用静态类型,因此编译器不需要确定detailRoute
的形式。选项3
另一个选项是您在自己的答案中所做的,因为您提供了类型注解,所以编译器也不需要确定
detailRoute
的形状。另请参阅
https://github.com/Microsoft/TypeScript/issues/5711
当试图发出[the module]时,编译器需要写一个对象类型文字......来表示模块的形状。但是在作用域中没有直接引用[Route]的名称,所以类型“cannot be named”,并且出现错误。
如果添加[Route]的[直接]导入...错误应该会消失。
ca1c2owp2#
显然这就是解决我问题的办法:
指定detailRoute是一个RouteConfig(它反过来使用Route)解决了这个问题。我一定是误解了它应该如何工作,但这修复了它。
ctzwtxfj3#
对我来说,这个问题是因为我试图建立一个库做:
我改口:
问题已解决。
qxsslcnc4#
我在输入rootReducer时遇到了这个问题,以防其他人也在做同样的事情。我正在导入由其他类型(状态、操作)组成的类型化reducer,我还没有导出这些类型。
简短回答:从reducer导出所有的action和state类型!
当复合类型的部分没有被导出并且依赖于类型推断时,复合类型似乎不能很好地工作。在这种情况下,推断rootReducer的类型(如果您有多个reducer,那么显式类型就太多了)。
yc0p9oo05#
对于那些寻找答案的人来说,这是这个问题的延伸。
具备以下条件:
typescript
安装的版本:
^4.8.3
配置
包. json
布局
我在自己的图书馆遇到了这个问题。
代码
1.已使用导出的类型(
Box
)1.导出的类型使用了未导出的类型(
Dimension
)1.通过隐式类型使用导出的类型(没有显式
: SomeType
注解)1.声明
Box
为[已命名但不能为]时出错--(读取:“我找不到某物的名称”)原因
Typescript正在
Box
中查找名为Dimension
的类型,但失败了。“Cannot be named”是一个不明确的错误,但从抛出它的上下文来看,它基本上意味着“Yo,I have no clue what 's in this thing”。我的解决方案
汇出巢状型别。
应该成为
omvjsjqw6#
只需将其添加到
tsconfig.json
中oiopk7p57#
添加返回类型为我解决了这个问题
给我错误
SomeType
这修复了问题: