Typescript ';选择';仅引用类型,但在尝试扩展Pick时用作此处的值<;

Typescript ';选择';仅引用类型,但在尝试扩展Pick时用作此处的值<;,typescript,Typescript,我只是想让我的后代知道祖先的一些属性。我试图通过Pick export class Base { public a; public b; public c; } export class PartialDescendant extends Pick<Base, 'a' |'b'> { public y; } 导出类基{ 公共a; 公共b; 公共c; } 导出类PartialDescendant扩展拾取{ 公共图书馆; } 但我收到两个错误- 错误:

我只是想让我的后代知道祖先的一些属性。我试图通过
Pick

export class Base {
    public a;
    public b;
    public c;
}

export class PartialDescendant extends Pick<Base, 'a' |'b'> {
   public y;
}
导出类基{
公共a;
公共b;
公共c;
}
导出类PartialDescendant扩展拾取{
公共图书馆;
}
但我收到两个错误-

错误:TS2693:“Pick”仅引用类型,但在此处用作值

错误:TS4020:导出类“PartialDescendant”的“extends”子句具有或正在使用私有名称“Pick”


我是否做错了什么,是否有其他方法仅公开基类的选定属性?

请参见下面的3.0解决方案

Pick
只是一个类型,而不是一个类,一个类既是一个类型又是一个对象构造函数。类型只在编译时存在,这就是为什么会出现错误

您可以创建一个函数,该函数接受一个构造函数,并返回一个新的构造函数,该构造函数将实例化一个字段较少的对象(或者至少声明它有):

这不仅缩短了时间,而且解决了许多问题

  • 可选参数仍然是可选的
  • 参数名被保留
  • 适用于任意数量的参数

Base包含的逻辑少于child。因此,从建筑学的角度来看,这似乎是错误的。您可以使用mixin或一些decorator向类中只添加所需的属性。在我的情况下,之所以发生这种情况,是因为为组件指定的道具不是可选的,我没有提供这些道具。谢谢,这正是我想要的。请您指出我如何将构造函数参数传递给基类。看到你给我的链接,我有点不知所措me@Anarion将解决方案改编为link@Anarion还修复了缺少的静态问题。
export class Base {
    public c: number = 0;
    constructor(public a: number, public b: number) {

    }
}


function pickConstructor<T extends { new (...args: any[]) : any, prototype: any }>(ctor: T)
    : <TKeys extends keyof InstanceType<T>>(...keys: TKeys[]) => ReplaceInstanceType<T, Pick<InstanceType<T>, TKeys>> & { [P in keyof Omit<T, 'prototype'>] : T[P] } {
    return function (keys: string) { return ctor as any };
}

export class PartialDescendant extends pickConstructor(Base)("a", "b") {
    public constructor(a: number, b: number) {
        super(a, b)
    }
}

var r = new PartialDescendant(0,1);

type IsValidArg<T> = T extends object ? keyof T extends never ? false : true : true;
type ReplaceInstanceType<T, TNewInstance> = T extends new (a: infer A, b: infer B, c: infer C, d: infer D, e: infer E, f: infer F, g: infer G, h: infer H, i: infer I, j: infer J) => infer R ? (
    IsValidArg<J> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => TNewInstance :
    IsValidArg<I> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => TNewInstance :
    IsValidArg<H> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => TNewInstance :
    IsValidArg<G> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => TNewInstance :
    IsValidArg<F> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F) => TNewInstance :
    IsValidArg<E> extends true ? new (a: A, b: B, c: C, d: D, e: E) => TNewInstance :
    IsValidArg<D> extends true ? new (a: A, b: B, c: C, d: D) => TNewInstance :
    IsValidArg<C> extends true ? new (a: A, b: B, c: C) => TNewInstance :
    IsValidArg<B> extends true ? new (a: A, b: B) => TNewInstance :
    IsValidArg<A> extends true ? new (a: A) => TNewInstance :
    new () => TNewInstance
) : never
export class Base {
    public c: number = 0;
    constructor(public a: number, public b: number) {

    }
}

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
function pickConstructor<T extends { new (...args: any[]) : any, prototype: any }>(ctor: T)
    : <TKeys extends keyof InstanceType<T>>(...keys: TKeys[]) => ReplaceInstanceType<T, Pick<InstanceType<T>, TKeys>> & { [P in keyof Omit<T, 'prototype'>] : T[P] } {
    return function (keys: string| symbol | number) { return ctor as any };
}

export class PartialDescendant extends pickConstructor(Base)("a", "b") {
    public constructor(a: number, b: number) {
        super(a, b)
    }
}

var r = new PartialDescendant(0,1);


type ArgumentTypes<T> = T extends new (... args: infer U ) => any ? U: never;
type ReplaceInstanceType<T, TNewInstance> = T extends new (...args: any[])=> any ? new (...a: ArgumentTypes<T>) => TNewInstance : never;