Record<K,T>;
将petsGroup中的每个值(‘dog’ | ‘cat’ | ‘fish’)都转为 IPetInfo 类型。当然也可以自己在第一个参数后追加额外的值,
type petsGroup = 'dog' | 'cat' | 'fish';
interface IPetInfo {
name:string,
age:number,
}
type IPets = Record<petsGroup, IPetInfo>;
const animalsInfo:IPets = {
dog:{
name:'dogName',
age:2
},
cat:{
name:'catName',
age:3
},
fish:{
name:'fishName',
age:5
}
}
网易云API 关键字搜索歌曲定义 Type
返回值示例:

使用:
/*
//代码片段
const keywords = ref(""); //关键字
const musicUrl = ref(""); //播放音乐的Url
const songsList: Songs[] = ref([]);
const searchClick = async (): void => {
const {
data: { result },
}: { data: Rows<Songs> } = await searchMusic({
keywords: keywords.value,
});
const musicId: number = result.songs[0].id;
console.log(result);
songsList.value = result.songs;
musicUrl.value = `https://music.163.com/song/media/outer/url?id=${musicId}.mp3`;
*/
定义 Type:
export type Rows<T> = {
result: Result<T>;
code: number;
};
type Result<T> = {
hasMore: boolean;
songCount: number;
songs: Array<T>;
};
type Songs = {
album: album;
alias: Array<string>;
artists: Array<artist>;
copyrightId: number;
duration: number;
fee: number;
ftype: number;
id: number;
mark: number;
mvid: number;
name: string;
rUrl: unknown;
rtype: number;
status: number;
};
type album = {
artist: Array<artist>;
copyrightId: number;
id: number;
mark: number;
name: string;
picId: number;
publishTime: number;
size: number;
status: number;
};
type artist = {
albumSize: number;
alias: Array<unknown>;
id: number;
img1v1: number;
img1v1Url: string;
name: string;
picId: number;
picUrl: string;
trans: string | number;
};

本文介绍如何将 petsGroup 中的 'dog', 'cat', 'fish' 转换为 IPetInfo 结构,以便统一管理宠物信息,包括名称和年龄。同时,展示了如何在 API 搜索中应用类似的数据类型定义。
9783

被折叠的 条评论
为什么被折叠?



