[英]Property does not exist on type 'TypedResponse<never>'.ts(2339)
I have a server side data loader that retrieves a user's ID from a data model, then checks that the user ID actually exists here:我有一个服务器端数据加载器,它从数据 model 中检索用户 ID,然后检查用户 ID 是否确实存在于此处:
export const loader = async ({ request, params }: LoaderArgs) => {
const REQUESTED_USER = await GetUserById(Number(params.id))
if (!REQUESTED_USER) {
/**
* If the provided url params user ID does not exist, we will handle it here
*/
return redirectWithError({
request,
message: `User ID: ${params.id} was not found; please enter a valid User ID`,
logMessage: `User ID: ${params.id} was not found; please enter a valid User ID`,
redirectTo: '/manager'
})
}
}
REQUESTED_USER returns: REQUESTED_USER 返回:
const REQUESTED_USER: {
id: number;
company_name: string | null;
avatar: string | null;
brand: string | null;
email: string;
name: string | null;
favicon: string | null;
} | null
There is code after that block that does some data manipulation, then returns the data in a typed JSON response:该块之后的代码执行一些数据操作,然后在类型化的 JSON 响应中返回数据:
if (GET_SELECTED_SERVICES) {
sma = unwrap_sma[0]
edr = unwrap_edr[0]
cma = unwrap_cma[0]
vms = unwrap_vms[0]
}
return typedjson({
sma,
vms,
cma,
edr,
user
})
After which I am passing it to a component in React on the client side:之后我将它传递给客户端的 React 组件:
export const ReadUser = ({ }) => {
const LOADER = useTypedLoaderData<typeof loader>();
const [CSRF] = useCSRF()
return (
<main className="flex w-full">
<Form method="post" className="flex w-full">
<input className="hidden" name={'csrf'} value={CSRF} type="text" readOnly />
<Profile SMA={LOADER.sma} EDR={[]} VMS={[]} CMA={[]} />
</Form>
</main>
)
}
However, the attributes on the Profile
component are throwing this error:但是,
Profile
组件上的属性抛出此错误:
Property 'sma' does not exist on type 'TypedResponse<never> | TypedJsonResponse<{ sma: TOptions[]; vms: TOptions[]; cma: TOptions[]; edr: TOptions[]; user: { id: number; company_name: string | null; ... 4 more ...; favicon: string | null; }; }>'.
Property 'sma' does not exist on type 'TypedResponse<never>'.ts(2339)
I have determined it is because of the initial REQUESTED_USER
guard in the server-side loader
function, whereby the LOADER
response is never fulfilled.我确定这是因为服务器端
loader
function 中的初始REQUESTED_USER
守卫,因此LOADER
响应永远不会实现。 I assume?我假设? this is due to Typescript not understanding the fulfillment of the request, but not 100% on that (if someone could elucidate on that).
这是由于 Typescript 不理解请求的实现,但不是 100%(如果有人可以对此进行说明)。
Otherwise, how do I go about fixing this?否则,我如何解决此问题 go?
The type of the LOADER
const in ReadUser
component is: ReadUser
组件中LOADER
const 的类型是:
const LOADER: TypedResponse<never> | TypedJsonResponse<{
sma: TOptions[];
vms: TOptions[];
cma: TOptions[];
edr: TOptions[];
user: {
id: number;
company_name: string | null;
... 4 more ...;
favicon: string | null;
};
}>
I did try to change the !REQUESTED_USER
argument to REQUESTED_USER === null
and that did not change anything我确实尝试将
!REQUESTED_USER
参数更改为REQUESTED_USER === null
并且没有任何改变
I assume your loader can return types A = {x:1}
and B = {y:2}
, that results in union A | B
我假设您的加载程序可以返回类型
A = {x:1}
和B = {y:2}
,这会导致 union A | B
A | B
type for typeof loader
. A | B
类型为typeof loader
。 If you try to access LOADER.x
ts will complain because LOADER
can be of type B
and there is no property x
on it, same the other way around.如果你尝试访问
LOADER.x
ts 会报错,因为LOADER
可以是B
类型并且它上面没有属性x
,反之亦然。
As types are inferred correctly by what you actually return and there is no pretty way to discriminate union for or case , you can only assign type manually useTypedLoaderData() as A
or throw redirect that will remove first return type from union and seems also work in remix - throw redirect('/somewhere')
由于类型是根据您实际返回的内容正确推断出来的,并且没有很好的方法来区分union 或 case ,您只能手动分配类型
useTypedLoaderData() as A
或抛出重定向,这将从 union 中删除第一个返回类型并且似乎也适用于混音 - throw redirect('/somewhere')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.