代码之家  ›  专栏  ›  技术社区  ›  Joff

React+Typescript:类型中缺少render prop属性*

  •  1
  • Joff  · 技术社区  · 6 年前

    有人能告诉我这个设置有什么问题吗。我做了一个小包装 react-intl 的注入组件,所以我可以使用它作为渲染道具,但编译器正在向我抱怨。。。

    interface Props {
      intl: InjectedIntl;
      children(props: InjectedIntl): JSX.Element;
    }
    
    class Intl extends React.Component<Props, {}> {
      public render() {
        return this.props.children(this.props.intl);
      }
    }
    
    export default injectIntl(Intl);
    

    然后渲染道具是这样的。。。

      <Intl> // <-- compiler complaining on this line
        {(intl) => (
          <div>
           {intl.messages.iCanHazRenderProp}
          </div>
        )}
      </Intl>
    

    Type '{ children: (intl: InjectedIntl) => Element; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<Props, ComponentState, any>> & Readonly<...'. Type '{ children: (intl: InjectedIntl) => Element; }' is not assignable to type 'Readonly<Props>'. Property 'intl' is missing in type '{ children: (intl: InjectedIntl) => Element; }'.
    

    编辑:injectIntl的签名

    (alias) injectIntl<Props>(component: ReactIntl.ComponentConstructor<Props & ReactIntl.InjectedIntlProps>, options?: ReactIntl.InjectIntlConfig | undefined): React.ComponentClass<Props> & {
    WrappedComponent: ReactIntl.ComponentConstructor<Props & ReactIntl.InjectedIntlProps>;
    

    } 导入injectIntl

    1 回复  |  直到 6 年前
        1
  •  0
  •   lazaruslarue Matyas Koszik    6 年前

    当我更改了 injectIntl

    ... WrappedComponent: React.ComponentType<IVisualizationProps & InjectedIntlProps>

    看起来@types/react intl正在返回一个不同的函数签名。。。

    https://github.com/DefinitelyTyped/DefinitelyTyped/commit/5e471f03c00b89ac358848e9cd66333466297f45