Typescript 类型脚本和泛型函数

Typescript 类型脚本和泛型函数,typescript,Typescript,希望这是一个简单的问题。 我创建了一个名为Page的模型,如下所示: export class Page implements Metadata { title: string; description: string; image: Image; slug: string; linkText: string; elements: Element[]; } public getPage(slug: string): Observable<Page> {

希望这是一个简单的问题。 我创建了一个名为Page的模型,如下所示:

export class Page implements Metadata {
  title: string;
  description: string;
  image: Image;

  slug: string;
  linkText: string;
  elements: Element[];
}
public getPage(slug: string): Observable<Page> {
  return from(
    this.client
      .getEntries({
        content_type: 'page',
        'fields.path[match]': slug,
        include: 3,
      })
      .then((response: any) =>
        {
          let page = response.find((item: any) => item.path === slug);
          if (!page) return;

          return this.createPage(page);
        }

      )
  );
}

public createPage(page: Entry<any>): Page {
  return {
    title: page.fields.title,
    description: page.fields.description,
    image: this.createImage(page.fields.image),
    slug: page.fields.path,
    linkText: page.fields.linkText,
    elements: page.fields.content?.map((content: any) =>
      this.createElement(content)
    ),
  };
}
然后我有一个方法,可以获取一个页面,并将其转换为如下所示:

export class Page implements Metadata {
  title: string;
  description: string;
  image: Image;

  slug: string;
  linkText: string;
  elements: Element[];
}
public getPage(slug: string): Observable<Page> {
  return from(
    this.client
      .getEntries({
        content_type: 'page',
        'fields.path[match]': slug,
        include: 3,
      })
      .then((response: any) =>
        {
          let page = response.find((item: any) => item.path === slug);
          if (!page) return;

          return this.createPage(page);
        }

      )
  );
}

public createPage(page: Entry<any>): Page {
  return {
    title: page.fields.title,
    description: page.fields.description,
    image: this.createImage(page.fields.image),
    slug: page.fields.path,
    linkText: page.fields.linkText,
    elements: page.fields.content?.map((content: any) =>
      this.createElement(content)
    ),
  };
}
所以我想改变我的方法:

public getPage<T extends Page>(slug: string, callback: (page: any) => T = this.createPage(page)): Observable<T> {
  return from(
    this.client
      .getEntries({
        content_type: 'page',
        'fields.path[match]': slug,
        include: 3,
      })
      .then((response: any) =>
        {
          let page = response.find((item: any) => item.path === slug);
          if (!page) return;

          return callback(page);
        }

      )
  );
}
publicGetPage(slug:string,callback:(page:any)=>T=this.createPage(page)):可观察{
归来(
这是我的客户
.getEntries({
内容类型:“页面”,
“fields.path[match]”:slug,
包括:3,,
})
。然后((响应:任意)=>
{
让page=response.find((item:any)=>item.path==slug);
如果(!page)返回;
返回回调(第页);
}
)
);
}
这里的问题是typescript抱怨:

类型“Page”不可分配给类型“(Page:any)=>T”。键入“Page”不提供与签名(Page:any):T)匹配的内容

我想它必须通过我的工厂方法,因此我将其更改为:

public createPage<T extends Page>(page: Entry<any>): T {
  return {
    title: page.fields.title,
    description: page.fields.description,
    image: this.createImage(page.fields.image),
    slug: page.fields.path,
    linkText: page.fields.linkText,
    elements: page.fields.content?.map((content: any) =>
      this.createElement(content)
    ),
  };
}
publicCreatePage(页面:条目):T{
返回{
标题:page.fields.title,
描述:page.fields.description,
image:this.createImage(page.fields.image),
slug:page.fields.path,
linkText:page.fields.linkText,
元素:page.fields.content?.map((内容:任意)=>
this.createElement(内容)
),
};
}
上面说:

类型“{title:any;description:any;image:image;slug:any;linkText:any;elements:any;}”不可分配给类型“T”。 “{title:any;description:any;image:image;slug:any;linkText:any;elements:any;}”可分配给类型为“T”的约束,但“T”可以用约束“Page”的不同子类型实例化

有人知道我做错了什么吗?

将签名更改为:

public getPage<T extends Page>(slug: string, callback: (page: Entry<any>) => T = this.createPage.bind(this)): Observable<T> {
publicGetPage(slug:string,callback:(page:Entry)=>T=this.createPage.bind(this)):可观察{
您能否将此问题简化为一个问题?