You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为react-native-ytdl补充getBasicInfo的TypeScript类型定义方法

补充react-native-ytdl的getBasicInfo类型定义,支持解构导入

直接修改node_modules/@types/react-native-ytdl/index.d.ts文件,添加getBasicInfo的类型声明和命名导出即可解决问题,具体操作如下:

  1. 打开该类型声明文件,先定义视频基础信息的类型接口(可根据实际返回字段调整补充):
interface VideoBasicInfo {
  title: string;
  videoId: string;
  lengthSeconds: string;
  thumbnail: Array<{
    url: string;
    width: number;
    height: number;
  }>;
  // 按需添加更多返回字段,比如作者、播放量等
  author?: {
    name: string;
    channelId: string;
  };
  viewCount?: string;
}
  1. 声明getBasicInfo函数的类型:
function getBasicInfo(url: string): Promise<VideoBasicInfo>;
  1. 最后添加命名导出语句:
export { getBasicInfo };

整合后的完整类型声明文件示例:

declare module 'react-native-ytdl' {
  interface VideoBasicInfo {
    title: string;
    videoId: string;
    lengthSeconds: string;
    thumbnail: Array<{
      url: string;
      width: number;
      height: number;
    }>;
    author?: {
      name: string;
      channelId: string;
    };
    viewCount?: string;
  }

  type YtdlOptions = {
    // 按需定义ytdl的配置选项类型
    quality?: string;
  };

  type YtdlFunction = (url: string, options?: YtdlOptions) => NodeJS.ReadableStream;

  function getBasicInfo(url: string): Promise<VideoBasicInfo>;

  const ytdl: YtdlFunction;
  export default ytdl;

  export { getBasicInfo };
}

修改完成后,就能正常使用import ytdl, {getBasicInfo} from 'react-native-ytdl';进行解构导入,调用getBasicInfo时也不会再出现类型报错。

内容的提问来源于stack exchange,提问作者Philippe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 03:45:31