typescript TS4023:导出的变量< x>具有或正在使用< y>来自外部模块的名称,但无法命名

fv2wmkja  于 2022-12-05  发布在  TypeScript
关注(0)|答案(7)|浏览(364)

我以前见过有人回答这个问题,但他们似乎没有涵盖这个特定的使用情形(或者他们不起作用/没有帮助)

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.

os8fio9y

os8fio9y1#

编译器无法计算出detailRoute的确切形状,因为它不知道Route的形状。

选项1

解决这个问题的一种方法是从源代码导入Route,从而提供编译器确定detailRoute的形状所需的信息。

import { Route } from "./../node_modules/vue-router/types/router";

export const detailRoute = {
  props: (route: Route) => null,
};

由于the index.d.ts file in vue-router(您在问题中导入的)重新导出Route,因此它不提供编译器所需的对Route的 * 直接 * 引用。

选项2

另一个选择是完全不使用detailRoute静态类型。

import { Route } from 'vue-router'; // index.d.ts

export const detailRoute: any = {
  props: (route: Route) => null,
};

由于any选择不使用静态类型,因此编译器不需要确定detailRoute的形式。
选项3
另一个选项是您在自己的答案中所做的,因为您提供了类型注解,所以编译器也不需要确定detailRoute的形状。

import { Route, RouteConfig } from 'vue-router'; // index.d.ts

export const detailRoute: RouteConfig = {
  props: (route: Route) => null,
};

另请参阅

https://github.com/Microsoft/TypeScript/issues/5711
当试图发出[the module]时,编译器需要写一个对象类型文字......来表示模块的形状。但是在作用域中没有直接引用[Route]的名称,所以类型“cannot be named”,并且出现错误。
如果添加[Route]的[直接]导入...错误应该会消失。

ca1c2owp

ca1c2owp2#

显然这就是解决我问题的办法:

import {Route, RouteConfig} from 'vue-router';

  export const detailRoute: RouteConfig = {
    path: '/detail/:id',
    component: Detail,
    props: (route: Route) => ({
      state: route.query.state
    })
  };

指定detailRoute是一个RouteConfig(它反过来使用Route)解决了这个问题。我一定是误解了它应该如何工作,但这修复了它。

ctzwtxfj

ctzwtxfj3#

对我来说,这个问题是因为我试图建立一个库做:

interface Props {...};
const MyComponent = ({...}:Props)=>{<>...</>}

我改口:

type Props = {...};

问题已解决。

qxsslcnc

qxsslcnc4#

我在输入rootReducer时遇到了这个问题,以防其他人也在做同样的事情。我正在导入由其他类型(状态、操作)组成的类型化reducer,我还没有导出这些类型。

简短回答:从reducer导出所有的actionstate类型!

当复合类型的部分没有被导出并且依赖于类型推断时,复合类型似乎不能很好地工作。在这种情况下,推断rootReducer的类型(如果您有多个reducer,那么显式类型就太多了)。

const rootReducer = combineReducers({ typedReducerA, typedReducerB, ... }
yc0p9oo0

yc0p9oo05#

对于那些寻找答案的人来说,这是这个问题的延伸。
具备以下条件:

typescript

安装的版本:^4.8.3
配置

{
  "module": "NodeNext",
  "moduleResolution": "NodeNext"
}
包. json
{
  "type": "module"
}

布局

src/lib/types.ts      // contains all type defs
src/lib/something.ts  // contains type def consumption and error

我在自己的图书馆遇到了这个问题。

代码

1.已使用导出的类型(Box
1.导出的类型使用了未导出的类型(Dimension
1.通过隐式类型使用导出的类型(没有显式: SomeType注解)
1.声明Box为[已命名但不能为]时出错--(读取:“我找不到某物的名称”)

原因

Typescript正在Box中查找名为Dimension的类型,但失败了。“Cannot be named”是一个不明确的错误,但从抛出它的上下文来看,它基本上意味着“Yo,I have no clue what 's in this thing”。

我的解决方案

汇出巢状型别。

export interface Box {
  width: Dimension;
}

interface Dimension {
  size: number;
  meta: any;
}

应该成为

export interface Box {
  width: Dimension;
}

// Update this:
//     interface Dimension {
// To this:
export interface Dimension {
  size: number;
  meta: any;
}
omvjsjqw

omvjsjqw6#

只需将其添加到tsconfig.json

compilerOptions: {
  ...
  "declaration": false,
  "emitDeclarationOnly": false
}
oiopk7p5

oiopk7p57#

添加返回类型为我解决了这个问题

export const test_setUpApp = async (args?: {
    fixtureData: SomeType;
}) => {
    ....
    }

给我错误SomeType这修复了问题:

export const test_setUpApp = async (args?: {
    fixtureData: SomeType;
}):Promise<ReturnType> => {
    ....
    }

相关问题