I'd like to destructure type one level down:
type Full = {
firstLevel: {
secondLevel: {
value: string
}
}
}
type Desired = {
secondLevel: {
value: string
}
}
I've tried with
type Deconstruct<T,K extends keyof T> = {
[P in T[K]]: T[K][P];
}
but it doesn't compile. Any ideas without extracting one level down type as separate?
Here are some different ways to achieve what you want: playground
type Full = {
firstLevel: {
secondLevel: {
value: string;
};
};
};
// Manual
type DesiredA = Full['firstLevel'];
// Generic deconstruct type
type Deconstruct<T, K extends keyof T = keyof T> = T[K];
// Deconstruct every key
type DesiredB = Deconstruct<Full>;
// Deconstruct only one specific key
type DesiredC = Deconstruct<Full, 'firstLevel'>;
I've already created a solution (oh, it's the most complicated one from all answers:D)
type StripOneLevel<T, K extends keyof T> = {
[P in keyof T[K]]: T[K][P];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.