ts 已写 interface 声明 却提示 type 声明未定义

1.添加了一个全局声明文件


declare interface LocalFile {
  // 文件名
  // : "Aragami_t1641735966693.zip"
  basename: string;
  // 文件夹名称
  // Aragami
  dirname: string;
  // 文件绝对路径
  // "C:\\my_git_project\\game-document-sync\\backup\\Aragami\\Aragami_t1641735966693.zip",
  path: string;
  // 文件大小(字节)
  size: number;
  // 时间戳
  timeStamp: number;
  // 文件类型
  type: 'file' | 'directory';
}

declare interface LocalFileFormat extends LocalFile {
  // 用于比较同步文名标识: = 本地文件名: 格式规范 = (文件夹/文件名) = Aragami/Aragami_t1641735966693.zip
  comparsedName: string;
}

2.文件引用提示错误,未找到声明,如图
image.png

是声明方式不对还是...

阅读 3.8k
3 个回答
  • 全局声明要看这个配置项:typeRoots,默认是 @types。参考 https://www.typescriptlang.or...
  • 不需要全局声明的话,也可以手工 import 进去
  • 另外,经过函数调用后,返回的是什么类型,需要看函数的类型声明,比如你的:unref(directoryItem)

全局声明文件有 import export 等关键字吗,有的话,一般会识别成模块文件,而不是类型声明文件。
上述不是的话,建议排查下 tsconfig.json 文件配置。

新手上路,请多包涵
  1. 在tsconfig.json中配置typesRoot
  2. 手动导入
  3. 创建同名的.d.ts文件,vscode会自适应

    // ./src/test.d.ts
    declare interface config {
    path: string;
    }
    // ./src/test.ts
    let config:config = {
      path: ""
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题