Reactjs 反应&;类型脚本从接口定义中排除

Reactjs 反应&;类型脚本从接口定义中排除,reactjs,typescript,definition,Reactjs,Typescript,Definition,经过几个小时的测试,我问你。我想定义我的接口对象 export interface OptionPros { text: string; color?: string|undefined; fontStyle?: string|undefined; sidePadding?: number|undefined; } 之后,我想创建一个只使用“text”属性的类型,另一个是可选的 type OO=Exclude<"color"|"fontStyle"|"si

经过几个小时的测试,我问你。我想定义我的接口对象

export interface OptionPros {
    text: string;
    color?: string|undefined;
    fontStyle?: string|undefined;
    sidePadding?: number|undefined;
}
之后,我想创建一个只使用“text”属性的类型,另一个是可选的

type OO=Exclude<"color"|"fontStyle"|"sidePadding", OptionPros>
type OO=Exclude
现在一切都好了,但当我试着在我的课堂道具中使用这个定义时

file1.tsx

export interface OptionPros {
    text: string;
    color?: string|undefined;
    fontStyle?: string|undefined;
    sidePadding?: number|undefined;
}
type OO=Exclude<"color"|"fontStyle"|"sidePadding", OptionPros>

export interface DProps extends BaseProps {
    optionD: OO
}
export default class DDD <DProps> {
    static defaultProps = {
        optionD: {
            text: '',
            color: '#FF6384', // Default is #000000
            fontStyle: 'Arial', // Default is Arial
            sidePadding: 20 // Defualt is 20 (as a percentage)
        }
    };
    render() {<div>AAAA</div>}
 }
 //i omit react and other inclusion or extension
 import.....
 export default class AAAA {
 render() {return(<DDD optionD={{text:"hello world"}}>AAAA</DDD >)}
导出接口选项PROS{
文本:字符串;
颜色?:字符串|未定义;
fontStyle?:字符串|未定义;
侧边填充?:编号|未定义;
}
类型OO=排除
导出接口DProps扩展BaseProps{
选项:OO
}
导出默认类DDD{
静态defaultProps={
选项D:{
文本:“”,
颜色:'#FF6384',//默认值为#000000
fontStyle:'Arial',//默认值为Arial
侧边填充:20//default为20(百分比)
}
};
render(){AAAA}
}
file2.tsx

export interface OptionPros {
    text: string;
    color?: string|undefined;
    fontStyle?: string|undefined;
    sidePadding?: number|undefined;
}
type OO=Exclude<"color"|"fontStyle"|"sidePadding", OptionPros>

export interface DProps extends BaseProps {
    optionD: OO
}
export default class DDD <DProps> {
    static defaultProps = {
        optionD: {
            text: '',
            color: '#FF6384', // Default is #000000
            fontStyle: 'Arial', // Default is Arial
            sidePadding: 20 // Defualt is 20 (as a percentage)
        }
    };
    render() {<div>AAAA</div>}
 }
 //i omit react and other inclusion or extension
 import.....
 export default class AAAA {
 render() {return(<DDD optionD={{text:"hello world"}}>AAAA</DDD >)}
//我省略了react和其他包含或扩展
进口。。。。。
导出默认类别AAAA{
render(){return(AAAA)}
我犯了这个错误

类型“{text:string;}”不能分配给类型“{text:string;color:string;fontStyle:string;sidePadding:number;}”。 类型“{text:string;}”中缺少属性“color”。


我不明白为什么?我使用的是typescript>2.8,ts文档不太清楚。有人能帮我解决并消除我的错误吗?

我相信我已经成功地再现了错误。您的组件声明应该是:

export default class DDD extends React.Component<DProps> { ... }
//                       ^^^^^^^^^^^^^^^^^^^^^^^

通过这些更改,我不再收到错误。

谢谢Matt,但是选择什么是错误?你能给我一个url,让我准确阅读以理解T,K e Pick吗?有一些关于“实用程序类型”的文档。此页面是最近添加的,我预计最终将部署到。有关如何定义这些类型的更多背景信息,请参阅和。