根据Typescript中的类型实现特定行为

Ewaren

我正在构建一个我想在其中显示文章的React网站(我称它们为“预览”)。

文章可以是文本,也可以是图像/视频。我宣布两个接口(TextPreviewPropsImgVidPreviewProps某些共同的属性() ,id),type但也对其他人的不同(TextPreviewProps具有text的特性,同时ImgVidPreviewPropsfile例如属性)。

export interface TextPreviewProps {
    id: number;
    type: "text";
    text: string;
    onChangeText?: (newText: string) => void;
}

export interface ImgVidPreviewProps {
    id: number;
    type: "image" | "video";
    file: File;
}

export type ContentPreviewProps = TextPreviewProps | ImgVidPreviewProps;

我已经声明了类型别名ContentPreviewProps,它可以是TextPreviewPropsImgVidPreviewProps

我的呈现“预览”的函数将ContentPreviewProps用作输入,并且我希望它根据输入是Text还是Image / Video返回一个不同的React Component。由于我们不能真正地在Typescript中动态地进行类型检查,因此最好的方法是什么?

此刻,我检查type属性是“ text”还是“ image” /“ video”,然后根据的值ContentPreviewProps对象转换为TextPreviewPropsImgVidPreviewPropstype

const renderPreview = (
        content: ContentPreviewProps
    ): JSX.Element => {
        const { type, id } = content;

        if (type === "text") {
            const textContent = content as TextPreviewProps;
            return (
                <TextPreview
                    type={type}
                    text={textContent.text}
                    onChangeText={(newText: string) => console.log(newText)}
                    id={id}
                />
            );
        }

        const imgVidContent = content as ImgVidPreviewProps;
        return (
            <ImgVidPreview
                type={type}
                file={imgVidContent.file}
                id={id}
            />
        );
    };

但是,这种方法存在冗余,因为我必须同时检查type和根据检查结果转换对象。

在Typescript中在运行时实现此类特定于类型的行为的惯用方式是什么?

苏丹·H

当您这样做时:

export type ContentPreviewProps = TextPreviewProps | ImgVidPreviewProps;

然后,从type两者中的prop出发TextPreviewPropsImgVidPreviewProps将确定将在type中指向哪一个ContentPreviewProps

因此,当您收到prop时type: string = "text"ContentPreviewProps习惯上将是:

{
    id: number;
    type: "text";
    text: string;
    onChangeText?: (newText: string) => void;
}

因此,在使用时,您不必根据type道具值进行投射因为您在声明该类型时已经这样做了:

export type ContentPreviewProps = TextPreviewProps | ImgVidPreviewProps;

您可以将代码修改为:

从Ewaren进行的AS更新在下面,破坏typecontent是问题的原因。

const renderPreview = (
        content: ContentPreviewProps
    ): JSX.Element => {
        if (content.type === "text") { // TS won't complain about textContent.text since you are validating it here.
            return (
                <TextPreview
                    type={content.type}
                    text={content.text}
                    onChangeText={(newText: string) => console.log(newText)}
                    id={content.id}
                />
            );
        }

        return (
            <ImgVidPreview
                type={content.type}
                file={content.file}
                id={content.id}
            />
        );
    };

我相信TS不会对此抱怨。因为您正在有条件地检查type

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeScript:结合类型和实现的行为

oop-C ++-实现特定于类型的行为的正确方法?

Typescript从实现中推断通用类型

是否可以根据实现的类型类为泛型定义不同的行为?

根据 TypeScript 中的查找类型限制通用键类型

根据 Typescript 中同级属性的泛型类型推断类型

TypeScript混合类型实现

根据通用接口的实现来限制Java中的通用类型

强制转换为数据类型的指针时实现的特定行为

根据TypeScript中的参数在字典中定义函数的返回类型

TypeScript推断接口实现类中的类型参数

Typescript 接口类型变量,在数组中实现

TypeScript中的联合类型-我可以实现吗?

TypeScript在静态实现中推断出更通用的类型

如何在 Typescript 中实现两种类型的比较

在 TypeScript 中为映射类型的特定属性设置类型

根据TypeScript中的参数动态生成返回类型

根据Typescript中的字符串推断类型

如何根据 switch case 为 TypeScript 中的对象分配类型

根据Typescript中的对象数组创建对象类型

如何根据TypeScript中的通用类型值添加字段?

根据TypeScript中的参数值更改返回类型

根据Typescript中多个对象的同名属性创建对象类型?

根据列表中对象的属性定义Typescript类型

如何根据Typescript中的变量设置动态类型

Java中具有特定类型的通用接口的实现

如何获得在Rust中实现特定特征的类型列表?

在Rust中为特定类型实现结构函数

TypeScript 中的类行为