typescript NextJS getInitialProps未按预期方式传递,并且出现编译器错误“varName not found on type {}”

dhxwm5r4  于 2022-12-14  发布在  TypeScript
关注(0)|答案(1)|浏览(70)

我想我有一个简单的语法问题,这是阻碍我的NextJ硬现在。
我尝试执行一些动态服务器端获取,因此我尝试了getInitialProps模式,但编译器无法识别正常功能组件render中getInitialProps的返回:

interface Props {
    date: string,
    user: number
}

const DayTimeView: NextComponentType<Props> = ({timeRecords, date, user}) => {

    return (
        <div>
            <h1>DayTimeView</h1>
        </div>
    )
}

DayTimeView.getInitialProps  = async (props: Props) => {
    let sesh = getSession();
    let timeRecords = []
    getSession().then((session) => {fetch('http://localhost:3000/api/time/date/' +new Date(props.date).toISOString(), {
        method: 'GET',
        headers: { "Content-Type": "application/json",
                    "x-auth-token": session.user.id},
        })
    .then(res => res.json().then(data => {
        if (data.length > 0) {
            timeRecords = data;
        }else{
            timeRecords = []; // Todo default
        }
    }))})
    return {props: { timeRecords: timeRecords, date: props.date, user: props.user }}
    
}

export default DayTimeView;

下面是编译器错误:

这并不是说属性中不存在timeRecords,因为即使在删除日期和用户上相同错误的timeRecords时,它也不起作用。

r3i60tvu

r3i60tvu1#

DayTimeView的类型应为NextPage,该类型是还包括NextJS上下文的NextComponentType

import { NextPage } from 'next';
const DayTimeView: NextPage<Props> = ...

相关问题