From ```T```, pick a set of properties whose type are not assignable to ```U```. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll build an OmitByType utility that filters out properties from an object type T whose values are assignable to a given type U.
From T, pick a set of properties whose type are not assignable to U.
For Example
type OmitBoolean = OmitByType<{
name: string
count: number
isReadonly: boolean
isEnable: boolean
}, boolean> // { name: string; count: number }Change the following code to make the test cases pass (no type check errors).
interface Model {
name: string
count: number
isReadonly: boolean
isEnable: boolean
}
type cases = [
Expect<Equal<OmitByType<Model, boolean>, { name: string; count: number }>>,
Expect<
Equal<
OmitByType<Model, string>,
{ count: number; isReadonly: boolean; isEnable: boolean }
>
>,
Expect<
Equal<
OmitByType<Model, number>,
{ name: string; isReadonly: boolean; isEnable: boolean }
>
>,
]
Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.
One-time payment. Lifetime access.
type OmitByType<T, U> = {
[K in keyof T as T[K] extends U ? never : K]: T[K]
}How it works:
K in T using a mapped type.as clause performs key remapping: for each key, we check if T[K] extends U.U, we remap the key to never, which effectively removes it from the resulting type.T[K].This challenge helps you understand key remapping in mapped types and how to apply conditional filtering of object properties based on their value types in real-world scenarios.
This challenge is originally from here.