I am facing an unusual scenario where I require a class property solely to retain a generic type without the intention of accessing it at runtime.
Here is an example of my class and utility type:
class Foo<T> {
private _dummy!: T;
}
type UnwrapFoo<T extends Foo<any>> = T extends Foo<infer Unwrapped> ? Unwrapped : never;
The _dummy
property is not utilized, but it is essential for the functioning of UnwrapFoo
. Without it, the type T
would be discarded, causing UnwrapFoo
to lose its inference capabilities.
My predicament arises when TypeScript generates the corresponding .d.ts
file. The type of _dummy
gets erased due to it being a private property. The generated typings end up looking like this:
class Foo<T> {
private _dummy;
}
Consequently, T
gets discarded once again and UnwrapFoo
stops functioning properly. Is there a suitable solution to preserve the type of _dummy
? Making it public is an option, but I prefer not to do so since it should remain internal and inaccessible to users of the Foo
class (also, it always holds the value undefined
regardless of its declared type).