IDE 没有找到声明的全局类型

IDE doesn't find the declared global types

我有一个全局类型文件,我在其中定义我的类型、变量等...

现在项目结构是这样的:

trunk
   typings
     index.d.ts
   src
     Example.ts
     Example.d.ts
   tsconfig.json

index.d.ts中我假设

declare type userInfo = {
    username: string,
    password: string,
}

但是在 Example.d.ts 中,当我直接使用 userInfo 时, IDE 说找不到名称,而 tsc 编译器没有显示错误。

declare class Something {
   ...
   getUserInfo: () => userInfo; // <--- this is highlighted red
}

有趣的是,当我在 Example.ts 中使用 userInfo 时,没有突出显示的错误。

另一个有趣的事情是当我 go to the declaration 它跳到 index.d.ts

中的正确行

我没有在这两个文件中导入类型,因为它们是 global 类型。

我的 tsconfig 文件如下所示:

{
    "compilerOptions": {
        ...
        "typeRoots": ["./typings"],
        ...
    },
    ...
}

可能是什么问题?

你不应该声明类型。当你想指出全局变量范围内有一些 javascript class 时,Declare 很有用(因此,Typescript 无法看到它)。例如:

declare class UserInfo {...} // typings.d.ts

现在您应该使用常规的 type

type UserInfo = {...} // Example.ts

更好的是,我建议你在你的情况下使用接口,因为 UserInfo 类型似乎很简单:

// Example.ts
interface UserInfo {
  username: string,
  password: string,
} 

有什么不明白的随时问我