简体   繁体   English

基于不同属性的多种TypeScript判别

[英]Multiple TypeScript discriminations based on different properties

I'm trying to build a complex REACT component which supports different use-cases.我正在尝试构建一个支持不同用例的复杂 REACT 组件。 In order to simplify its use, I want to implement TypeScript discriminations types to better infer the props.为了简化它的使用,我想实现 TypeScript 区分类型以更好地推断道具。

It's not useful to post the full example, but I can show you a simpler one, which is the following one:发布完整的示例没有用,但我可以向您展示一个更简单的示例,如下所示:

interface ICategoryDiscriminationGame {
    category: 'game';
    gameType: 'AAA' | 'AA' | 'A';
}

interface ICategoryDiscriminationProgram {
    category: 'program';
    programType: 'software' | 'freeware';
}

type TCategoryDiscrimination = (ICategoryDiscriminationGame | ICategoryDiscriminationProgram);


interface ISaleDiscriminationPresale {
    saleType: 'pre-sale',
    preSaleCost: number;
}

interface ISaleDiscriminationRetailSale {
    saleType: 'retail',
    retailSaleCost: number;
}

type TSaleDiscrimination = (ISaleDiscriminationPresale | ISaleDiscriminationRetailSale);


type TExampleCompProps = TCategoryDiscrimination & TSaleDiscrimination;

export const ExampleComp = (props: TExampleCompProps) => {
    if (props.category === 'game') { // In here, intellisense infer only use 'props.category' and 'props.saleType' -> NICE
        console.log(props.gameType); // In here, intellisense infer also 'props.gameType' -> NICE
    }

    if (props.saleType === 'pre-sale') { // In here, intellisense infer only use 'props.category' and 'props.saleType' -> NICE
        console.log(props.preSaleCost); // In here, intellisense infer also 'props.preSaleCost' -> NICE
    }

    if (props.category === 'game' && props.saleType === 'retail') { // In here, intellisense infer only use 'props.category' and 'props.saleType' -> NICE
        console.log(props.retailSaleCost); // In here, intellisense infer also 'props.retailSaleCost' and 'props.gameType' -> NICE
        console.log(props.gameType); // In here, intellisense infer also 'props.retailSaleCost' and 'props.gameType' -> NICE
    }

    return <p>In example comp</p>
}

As you can see, inside the ExampleComp , the intellisense is brilliant, and works great.如您所见,在ExampleComp内部,智能感知非常出色,而且效果很好。 The problem is when I try to use the ExampleComp .问题是当我尝试使用ExampleComp
What I would expect is that, when I write <ExampleComp , the intellisense allows me only the props category and saleType , since the other ones cannot exist without first defining those two.我期望的是,当我编写<ExampleComp时,智能感知只允许我使用 props categorysaleType ,因为如果不先定义这两个,其他的就不能存在。 BUT, instead, it just suggests everything:但是,相反,它只是暗示了一切:

在此处输入图像描述

So, the question here is: what am I missing that does not make the intellisense works correctly ALSO in the props?所以,这里的问题是:我错过了什么不能使智能感知在道具中也能正常工作?

TS Playground link TS Playground 链接

That's just the nature of IntelliSense.这就是 IntelliSense 的本质。 Once you start to supply some of the combinations of the required props, the suggested ones will be narrowed to only the ones which are applicable to the current possible combination:一旦您开始提供所需道具的一些组合,建议的组合将缩小到仅适用于当前可能组合的组合:

Before adding some props:在添加一些道具之前: 在添加一些道具之前

After adding some props:添加一些道具后: 添加一些道具后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM