javascript - Typescript 中的私有继承等价物(仅包括或排除特定的类成员或属性)
问题描述
在 Typescript 中模拟私有继承的最佳方法是什么?具体来说,chid 类想要隐藏父类的某些成员。
例如,预期的解决方法应实现以下目标:
类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
解决方案
您不想使用继承,因为您不打算让 aCustomArray<T>
以所有相同的方式使用 an 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
这更乏味,但您可以对结果进行更多控制。不过,无论哪种方式都应该适合你。希望有帮助;祝你好运!