简体   繁体   English

“TypedResponse”类型上不存在属性<never> '.ts(2339)</never>

[英]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.

相关问题 类型“从不”上不存在属性“单击”。 TS2339 - Property 'click' does not exist on type 'never'. TS2339 Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339) NextJs 中的 axios 响应中的类型“never”.ts(2339) 上不存在属性 - Property does not exist on type 'never'.ts(2339) in axios response in NextJs TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 Angular 管道上的 .includes 返回“属性 &#39;includes&#39; 在类型 &#39;never&#39;.ts(2339)&#39; 上不存在错误消息 - .includes on Angular pipe is returning 'Property 'includes' does not exist on type 'never'.ts(2339)' error message TS2339:类型“ {}”上不存在属性“ props” - TS2339: Property 'props' does not exist on type '{}' 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 属性“模块”在类型“ typeof MyEditor”上不存在。ts(2339) - Property 'modules' does not exist on type 'typeof MyEditor'.ts(2339) Typescript 通用:“T”类型上不存在属性“pass”.ts(2339) - Typescript generic : Property 'pass' does not exist on type 'T'.ts(2339) 类型 'void | 上不存在属性 'subscribe' 可观察的<user> '。 ts(2339)</user> - Property 'subscribe' does not exist on type 'void | Observable<User>'. ts(2339)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM