在Typescript中等效的私有继承(仅包括或排除特定的类成员或属性)

问题描述 投票:1回答:1

在Typescript中模拟私有继承的最佳方法是什么?具体来说,子类想要隐藏父类的某些成员。

例如,预期的解决方法应该实现以下目标:

CustomArray<T>Array<T>延伸,隐藏特定成员只说pop()shift()

let c1 = new CustomArray<number>();
c1.push(10, 20, 30, 40, 50); // okay
c1.shift(); // should error
c1.pop();   // should error
c1.sort(); // okay  etc...

这是我尝试过的,但vscode仍然允许应该是受限制的成员。

//Try to hide pop() and push():
type T1<T> = Exclude<Array<T>, 'pop'| 'push'>

// check
let x: T1<number> = [];
x.push(3);  // allowed -- okay
x.pop();    // also allowed -- but wanted it to be an error
javascript typescript inheritance visual-studio-code typescript-generics
1个回答
3
投票

你不想使用继承,因为你不打算允许CustomArray<T>以相同的方式使用Array<T>

你可以做的是将你的新类型定义为Array<T>的函数,并使CustomArray构造函数在运行时与Array构造函数相同:

type CustomArray<T> = Pick<Array<T>, Exclude<keyof Array<T>, "shift" | "pop">>;
const CustomArray: new <T>() => CustomArray<T> = Array;

let c1 = new CustomArray<number>();
c1.push(10, 20, 30, 40, 50); // okay
c1.shift(); // error
c1.pop();   // error
c1.sort(); // okay 

这就像你问的那样。但请记住,这是Array<T>的“浅层”转型。例如,sort()方法仍将返回Array<T>,而不是CustomArray<T>

c1.sort().pop(); // okay

如果你真的想要一个“深度”转换,其中Array<T>的所有相关提及都被CustomArray<T>取代,你可能需要继续并手动指定完整的界面,因为自动映射不太可能按照你想要的方式工作:

interface CustomArray<T> {
  length: number;
  toString(): string;
  toLocaleString(): string;
  // pop(): T | undefined;
  push(...items: T[]): number;
  concat(...items: ConcatArray<T>[]): CustomArray<T>;
  concat(...items: (T | ConcatArray<T>)[]): CustomArray<T>;
  join(separator?: string): string;
  reverse(): CustomArray<T>;
  // shift(): T | undefined;
  slice(start?: number, end?: number): CustomArray<T>;
  sort(compareFn?: (a: T, b: T) => number): this;
  // ... ALL the other methods, omitted for brevity
} 
const CustomArray: new <T>() => CustomArray<T> = Array;
const c1 = new CustomArray();
c1.push(10, 20, 30, 40, 50); // okay
c1.shift(); // error
c1.pop();   // error
c1.sort(); // okay  
c1.sort().pop(); // error

这更乏味但你可以对结果进行更多的控制。但是,无论哪种方式都适合你。希望有所帮助;祝好运!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.