手动添加声明文件(TypeScript)

时间:2017-03-22 22:43:16

标签: javascript node.js typescript google-sheets

我没有找到“Google电子表格”的声明文件。所以我试图自己创造一个:

在打字中 - >模块,我添加了一个名为" google-spreadsheet"的新文件夹,然后我添加了一个名为" index.d.ts"的新文件。其中包含以下内容:

export class GoogleSpreadsheet {

}

在客户端文件中我有:

import * as GoogleSpreadsheet from 'google-spreadsheet';

但是' google-spreadsheet'标有红色,说:

  

TS2307无法找到模块' google-spreadsheet'。

BTW我已经安装了NPM-google-spreadsheet'它运行JavaScript。只有TypeScript困扰我。

任何想法如何解决?

2 个答案:

答案 0 :(得分:4)

如果你不需要关心这个模块中的内容,你只能创建一个*.d.ts文件并放入以下内容(例如创建一个typings.d.ts):

declare module 'google-spreadsheet';

要定义模块内的类型,您可以将上述代码更改为:

declare module 'google-spreadsheet' {
    // define the types...
}

答案 1 :(得分:0)

从项目的根目录:

  1. mkdir -p src/@types/google-spreadsheet - 为模块的类型声明创建一个文件夹。
  2. echo "declare module 'google-spreadsheet';" > src/@types/google-spreadsheet/index.d.ts

另外,确保您的 tsconfig 配置为使用 JSON returned from auth endpoint was invalid, yet status code was 200 包含 @types 文件夹。

{
  "compilerOptions": {
    "typeRoots": ["src/@types", "node_modules/@types"]
  }
}

此外,您可能还想看看 the typeRoots option

相关问题