首页 > 解决方案 > 如何在 googleapis 中使用 Typescript 类型(特别是使用 drive_v3 时的文件类型)

问题描述

我有这是我的package.json

  "dependencies": {
    "googleapis": "^50.0.0"
  }

我的index.ts文件中有这个:

import {drive_v3} from "googleapis";
const {Schema$File} = drive_v3.Schema$File
console.log({x:Schema$File})

tsc告诉我:

error TS2339: Property 'Schema$File' does not exist on type 'typeof drive_v3'.

但是当我查看.d.ts文件时,我可以看到是否在那里声明

使用 drive_v3 api 时导入“Google Drive File”类型的正确方法是什么?

标签: typescriptgoogle-api

解决方案


派对迟到了,但你可以这样做

 import { drive_v3 } from 'googleapis'

type DriveFile = drive_v3.Schema$File

const file: DriveFile = ....

或者只是使用drive_v3.Schema$File而不是重新分配它。


推荐阅读