Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 正在为现有HOC写入声明d.ts文件_Reactjs_Typescript - Fatal编程技术网

Reactjs 正在为现有HOC写入声明d.ts文件

Reactjs 正在为现有HOC写入声明d.ts文件,reactjs,typescript,Reactjs,Typescript,我在为HOC组件编写d.ts文件时遇到问题: 使用apollo client.js包装组件以插入道具。是这样的: export default App => { return class Apollo extends React.Component{ render(){ // code to get apolloClient variable omitted <Apollo client={apolloClient}> }

我在为HOC组件编写
d.ts
文件时遇到问题:
使用apollo client.js
包装组件以插入道具。是这样的:

export default App => {
  return class Apollo extends React.Component{
    render(){
       // code to get apolloClient variable omitted

       <Apollo client={apolloClient}>
    }
  }
}
在阿波罗客户机的
中。d.ts

import { ApolloClient } from 'apollo-boost';

export interface WithApolloClient {
    apolloClient: ApolloClient<any>;
}

export default <P extends WithApolloClient>(App: React.ComponentType<P>) => {
    return class Apollo extends React.Component<P> {};
};
从“apollo boost”导入{apollo客户端};
与客户端的导出接口{
阿波罗客户:阿波罗客户;
}
导出默认值(应用程序:React.ComponentType

)=>{ 返回类Apollo扩展React.Component

{}; };

TS正在抱怨:

`Type '{ id: string; }' is not assignable to type 'Readonly<Props>'.
  Property 'apolloClient' is missing in type '{ id: string; }'.`
`Type'{id:string;}'不可分配给类型'Readonly'。
类型“{id:string;}”中缺少属性“apolloClient”`

您需要从返回的React.Component中删除带有ApolloClient的接口。否则,它将期望一个客户端作为道具。像这样的方法应该会奏效:

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type Subtract<T, K> = Omit<T, keyof K>;

export default <P extends WithApolloClient>(App: React.ComponentType<P>) => {
  return class Apollo extends React.Component<Subtract<P, WithApolloClient>> {};
};
类型省略=拾取;
类型减法=省略;
导出默认值(应用程序:React.ComponentType

)=>{ 返回类Apollo扩展了React.Component{}; };


查看react apollo github,它似乎是用TS自己编写的:,与apollo客户机的
和apollo客户机的
有什么不同吗?我正在使用nextjs,下面是他们的示例:。它目前在JS中,所以当我尝试用apollo client.JS导入
时,它说我需要声明一个
d.ts
文件,这样就可以了。但是,在我导入HOC的文件中,我仍然需要导入要扩展的接口(
WithApolloClient
,在下面的示例中):
import WithApolloClient,{WithApolloClient}来自'src/lib/withapollo client'
接口道具使用ApolloClient{id?:string;}扩展
我是否必须在任何我想使用HOC的地方都这样做?如果你想使用需要将接口传递给react组件的属性。所以这正是要走的路。
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type Subtract<T, K> = Omit<T, keyof K>;

export default <P extends WithApolloClient>(App: React.ComponentType<P>) => {
  return class Apollo extends React.Component<Subtract<P, WithApolloClient>> {};
};