DefinitelyTyped:为现有组件添加新的道具类型 [英] DefinitelyTyped: Add new props typing for existing component

查看:101
本文介绍了DefinitelyTyped:为现有组件添加新的道具类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在DefinitelyTyped中为新的props声明新的类型?我在SelectField组件中用一些新的props更新了material-ui,但是在DefinitelyTyped中的键入是旧的.我可以通过某种方式扩展SelectField类型并添加新的props类型吗?现在我有:

Is there any way to declare new typing for new props within DefinitelyTyped? I updated material-ui with some new props in SelectField component, but typings in DefinitelyTyped are old. Can I extend in some way SelectField typing and add new props types? Now I have:

<SelectField
    multiple={true}
    hintText="Select type"
    value={[...this.state.values]}
    onChange={this.onChange}
    selectionRenderer={this.selectionRenderer}
>

我需要添加multiple?: booleanselectionRenderer: (values: any[]) => string类型.我尝试declare module 'material-ui/SelectField' {},但不起作用.有任何想法吗?

And I need to add multiple?: boolean and selectionRenderer: (values: any[]) => string types. I tried to declare module 'material-ui/SelectField' {} but it not works. Any ideas?

推荐答案

您应该可以使用模块扩充:

declare module "material-ui" {
    interface SelectFieldProps {
        multiple?: boolean;
        selectionRenderer: (values: any[]) => string;
    }
}

如您所见,语法与您尝试过的语法略有不同.

As you can see, the syntax is a bit sifferent than what you've tried.

如果在__MaterialUI命名空间中定义了SelectFieldProps,则此方法应该起作用:

If SelectFieldProps is defined in the __MaterialUI namespace, then this should work:

declare module "material-ui" {
    namepsace __MaterialUI {
        interface SelectFieldProps {
            multiple?: boolean;
            selectionRenderer: (values: any[]) => string;
        }
    }
}

这篇关于DefinitelyTyped:为现有组件添加新的道具类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆