为react-native-ytdl补充getBasicInfo的TypeScript类型定义方法
补充react-native-ytdl的getBasicInfo类型定义,支持解构导入
直接修改node_modules/@types/react-native-ytdl/index.d.ts文件,添加getBasicInfo的类型声明和命名导出即可解决问题,具体操作如下:
- 打开该类型声明文件,先定义视频基础信息的类型接口(可根据实际返回字段调整补充):
interface VideoBasicInfo { title: string; videoId: string; lengthSeconds: string; thumbnail: Array<{ url: string; width: number; height: number; }>; // 按需添加更多返回字段,比如作者、播放量等 author?: { name: string; channelId: string; }; viewCount?: string; }
- 声明
getBasicInfo函数的类型:
function getBasicInfo(url: string): Promise<VideoBasicInfo>;
- 最后添加命名导出语句:
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
相关产品推荐
相关产品推荐

