Typescript 如何用TS最好地处理getStaticProps场景?

Typescript 如何用TS最好地处理getStaticProps场景?,typescript,next.js,Typescript,Next.js,似乎是一个困难的场景 ctx对象有很多可选类型。例如,参数可以是数组或字符串+null |未定义 下面是我使用的一种简单方法,但它不包括将id作为超出getPostData(id:string | array)中类型定义的数组 有没有更好的办法 ///pages/posts/[id].tsx 从“下一步”导入{GetStaticProps,GetStaticPath}; ... export const getStaticProps:getStaticProps=async({params})=

似乎是一个困难的场景

ctx对象有很多可选类型。例如,参数可以是数组或字符串+null |未定义

下面是我使用的一种简单方法,但它不包括将
id
作为超出getPostData(id:string | array)中类型定义的数组

有没有更好的办法

///pages/posts/[id].tsx
从“下一步”导入{GetStaticProps,GetStaticPath};
...
export const getStaticProps:getStaticProps=async({params})=>{
//上面未指定参数类型
//使用params是为了确保params有一个id--?
//我们还从联合中删除了未定义类型
const postData=wait getPostData(params?.id!);
返回{
道具:{
postData,
},
};
};