是否可以在深度删除对象中键的所有实例的函数上保持类型覆盖?
我的功能看起来像这样。
data/ca-certificates.crt
有什么方法可以使function omitDeep<T extends object>(obj: T, key: string): TWithoutProvidedKey {
return JSON.parse(
JSON.stringify(obj),
(key: string, value: any) => key === "__typename" ? undefined : value
);
}
成为现实?
答案 0 :(得分:5)
这很容易做到,您只需要使用映射类型来递归属性即可:
<pre>
<a href="https://example.com/Link_1">Link 1</a>
<a href="https://example.com/Link_2">Link 2</a>
<a href="https://example.com/Link_3">Link 3</a>
<a href="https://example.com/Link_4">Link 4</a>
<a href="https://example.com/Link_5">Link 5</a>
</pre>
<script>
// Array of the anchor (a) elements.
const links = document.getElementsByTagName('a');
// Loop the array
// Yes you can use a foreach or whatever you want instead
for (var i=0;i<links.length;i++) {
// On each of the links add a click event listener
links[i].addEventListener('click', function(event) {
// this in the event is the element, I'm giving assigning it here for clarity
const link = this;
// preventDefault stops the link from being followed
event.preventDefault();
// Set the window.location.href to the link's href attribute.
window.location.href = link.getAttribute('href');
});
}
</script>
仅需注意,它在3.4上有效,最近对数组和元组上的映射类型的处理发生了变化,因此,根据版本的不同,您可能需要作为特殊情况来处理数组。
答案 1 :(得分:1)
对于那些使用更高版本的TS(我已经用TS3.8.3测试过)的用户来说,您需要从Titian的答案中插入DeepOmitHelper
。
type Primitive =
| string
| Function
| number
| boolean
| Symbol
| undefined
| null;
type DeepOmitArray<T extends any[], K> = {
[P in keyof T]: DeepOmit<T[P], K>;
};
export type DeepOmit<T, K> = T extends Primitive
? T
: {
[P in Exclude<keyof T, K>]: T[P] extends infer TP
? TP extends Primitive
? TP // leave primitives and functions alone
: TP extends any[]
? DeepOmitArray<TP, K> // Array special handling
: DeepOmit<TP, K>
: never;
};
答案 2 :(得分:1)
这里的答案令人鼓舞。我可以解决TypeScript 4.0的一些小问题。我将其保留为要点:https://gist.github.com/ahuggins-nhs/826906a58e4c1e59306bc0792e7826d1。希望这对某些人有帮助,尤其是那些想彻底解决偏公用事业的人。
/** Union of primitives to skip with deep omit utilities. */
type Primitive = string | Function | number | boolean | Symbol | undefined | null
/** Deeply omit members of an array of interface or array of type. */
export type DeepOmitArray<T extends any[], K> = {
[P in keyof T]: DeepOmit<T[P], K>
}
/** Deeply omit members of an interface or type. */
export type DeepOmit<T, K> = T extends Primitive ? T : {
[P in Exclude<keyof T, K>]: //extra level of indirection needed to trigger homomorhic behavior
T[P] extends infer TP ? // distribute over unions
TP extends Primitive ? TP : // leave primitives and functions alone
TP extends any[] ? DeepOmitArray<TP, K> : // Array special handling
DeepOmit<TP, K>
: never
}
/** Deeply omit members of an array of interface or array of type, making all members optional. */
export type PartialDeepOmitArray<T extends any[], K> = Partial<{
[P in Partial<keyof T>]: Partial<PartialDeepOmit<T[P], K>>
}>
/** Deeply omit members of an interface or type, making all members optional. */
export type PartialDeepOmit<T, K> = T extends Primitive ? T : Partial<{
[P in Exclude<keyof T, K>]: //extra level of indirection needed to trigger homomorhic behavior
T[P] extends infer TP ? // distribute over unions
TP extends Primitive ? TP : // leave primitives and functions alone
TP extends any[] ? PartialDeepOmitArray<TP, K> : // Array special handling
Partial<PartialDeepOmit<TP, K>>
: never
}>