English 中文(简体)
有条件的返回类型与否
原标题:conditional return type not valid with prisma

www.un.org/Depts/DGACM/index_french.htm 载于<>Express内的类型:

type SerializedFunction<Serialized, Unserialized> 
    = <B extends boolean>(serialize: B) 
    => Promise<B extends true 
        ? (Serialized   | null) 
        : (Unserialized | null)
    >;

附属于以下职能:

import type { examples } from "@prisma/client";

interface DataSerialized{
    foo: string;
    bar: number;
}

const data: SerializedFunction<DataSerialized, examples> = async serialize => {
    // Return non-serialized data.
    if(!serialized) return await prisma.examples.findFirst({ 
        // ...
    });

    const example = await prisma.examples.findFirst({
        // ...
        select: {
            foo: true,
            bar: true,
        }
    });

    if(!example) return null;
    return {
        ...example,
        bar: await doSomethingWithBar(bar)
    };
}

然而,在我试图落实这一点时,我收到以下<>捷潘普文错误:

Type examples | { 
    // The object specified in the first "findFirst" call...
}
is not assignable to type  B extends true ? DataSerialized : examples .

Type  examples  is not assignable to type  B extends true ? DataSerialized : examples 

在什么地方我错了?

问题回答

您所期待的是dis discrimination Union

其工作方式是,你根据“静态”价值界定了两种类型的相同结构(或基于不同歧视类型的多种选择),但有一个类型,即: 不分青红皂白的(决定因素)。 您通过“硬编码”在多边贸易体系类型(通常为一种类型(定义))中的一种价值(aka字面)来这样做,但在这种情况下,确实是假的,或“指示”的。

Pseudo,但你执行以下一类方式:

type DataSerialized = {...}
type ExampleType = {...}

type MyReturnType = {
   serialized: true;
   data: DataSerialized
} | {
   serialized: false;
   data: ExampleType
}

更多关于歧视的工会的示范文件





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签