Currently, I am tackling a straightforward school project and have a query regarding the possibility of excluding all properties of a specific type in TypeScript.
type Student = {
firstName: string
lastName: string
age: number
gender: Gender
courses: List<Course>
}
For instance, I aim to create a type that retains everything from Student except for "courses".
Can we construct a Type that mirrors Student but excludes all properties with a List type? It should be versatile enough to function across various types like:
type Program = {
name: string
errors: List<Error>
successes: List<Success>
warnings: List<Warning>
}
This scenario would result in a type containing only { name: string }
If anyone has insights on achieving this, or if it's even feasible, perhaps through conditional types, Omit if List?
All assistance is highly appreciated!
Update: including code example:
type WithoutList<T> = {
[K in keyof T]: T[K] extends List<any> ? never : T[K]
}
const select = <a, b extends keyof WithoutList<a>>(arg: a, ...keys: b[]) => {
return null!
}
const s1: Student = {
firstName: 's',
lastName: 's',
age: 22,
gender: 'female',
courses: List<Course>(),
}
select(s1, 'courses') // courses should not be available here!