I have successfully implemented this TypeScript code:
import ytdl from 'react-native-ytdl';
type DirectLink = {
url: string;
headers: any[];
};
type VideoFormat = {
itag: number;
url: string;
width: number;
height: number;
};
type BasicInfo = {
formats: VideoFormat[];
};
export default class YoutubeVideo {
static root: string = 'https://www.youtube.com/watch?v=';
async getDirectLink(id: string): Promise<DirectLink[]> {
const url = `${YoutubeVideo.root}${id}`;
return await ytdl(url, {quality: 'highestaudio'});
}
async getBasicInfo(id: string): Promise<BasicInfo> {
const url = `${YoutubeVideo.root}${id}`;
return await ytdl.getBasicInfo(url);
}
}
Now I want to enhance the type definitions, so I created a file at node_modules/@types/react-native-ytdl/index.d.ts with the content :
export default function ytdl(link: string, options: any): Promise<any>;
This change eliminates the error for
await ytdl(url, {quality: 'highestaudio'})
.
Next, I need to make adjustments for getBasicInfo
so that I can write ?
import ytdl, {getBasicInfo} from 'react-native-ytdl';
...
return await getBasicInfo(url);