When examining the provided code snippet, I aim to filter properties based on their data type:
interface Wrapper<T> {
x: T;
}
interface WrapperOpt<T> {
y?: T;
}
interface A {
a1: number;
a2: Wrapper<number>;
a3: WrapperOpt<number>;
b1?: string;
b2?: Wrapper<string>;
b3?: WrapperOpt<string>;
}
type Wrapped = { [key in keyof A]: A[key] extends Wrapper<infer T> ? T : never };
type WrappedOpt = { [key in keyof A]: A[key] extends WrapperOpt<infer T> ? T : never };
This setup effectively filters by the Wrapper
type, resulting in Wrapped
where a2
and b2
are not never
:
type Wrapped = {
a1: never;
a2: number;
a3: never;
b1?: never;
b2?: string;
b3?: never;
}
However, the same process encounters issues with the WrapperOpt
type, leading to the creation of WrappedOpt
with unexpected data types for a3
and
b3</code:</p>
<pre><code>type WrappedOpt = {
a1: {};
a2: {};
a3: {};
b1?: {};
b2?: {};
b3?: {};
}
The desired outcome is to have a3
and
b3</code exhibit the correct types while others are designated as <code>never
:
type Wrapped = {
a1: never;
a2: never;
a3: number;
b1?: never;
b2?: never;
b3?: string;
}
What steps can be taken to address this discrepancy?
Furthermore, future plans involve eliminating the never
entries in types, as outlined in this resource, to achieve a more streamlined approach:
type Wrapped = {
a2: number;
b2?: string;
}
type WrappedOpt = {
a3: number;
b3?: string;
}