简体   繁体   English

如何从外部.d.ts文件扩展TS注释?

[英]How to extend TS annotations from external .d.ts file?

I am using React with react-bootstrap framework and I don't know how to use types from react-bootstrap in my component. 我正在将React与react-bootstrap框架一起使用,我不知道如何在组件中使用react-bootstrap的类型。

Example of my Button component: 我的Button组件示例:

import React, {Component} from 'react';
import {Button as ReactButton} from 'react-bootstrap';

interface ButtonProps {
    title: string;
    icon: JSX.Element;
    variant: string;
}

class Button extends Component<ButtonProps> {
    render() {
        const {variant, title, icon} = this.props;

        return <ReactButton variant={variant}>
            {icon}
            {title}
        </ReactButton>;
    }
}

export default Button;

Using this code I get this issue: 使用此代码,我会遇到以下问题:

Error:(14, 29) TS2322: Type 'string' is not assignable to type '"link" | 错误:(14、29)TS2322:类型'string'无法分配给类型'“ link” | "primary" | “主要” | "secondary" | “中学” | "success" | “成功” | "danger" | “危险” | "warning" | “警告” | "info" | “信息” | "dark" | “黑暗” | "light" | “光” | "outline-primary" | “概述主要” | "outline-secondary" | “大纲中学” | "outline-success" | “概述成功” | "outline-danger" | “概述危险” | ... 4 more ... | ... 4更多... | undefined'. 未定义”。

So, how may I annotate variant attribute in my component without breaking external .d.ts? 因此,如何在不破坏外部.d.ts的情况下在组件中注释变量属性?

Thanks! 谢谢!

You can define the type of your variant in relation to the variant in ReactButton : 您可以相对于ReactButtonvariant定义variant的类型:

interface ButtonProps {
    title: string;
    icon: JSX.Element;
    variant: ReactButton['props']['variant'];
}

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

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