Is there a method to specify only a portion of the object type, while allowing the rest to be of any type? The primary objective is to have support for intelliSense for the specified part, with the added bonus of type-checking support. To demonstrate, let's start by defining a helper type:
type TupleToIntersection<T extends any[]> = {
[I in keyof T]: (x: T[I]) => void
}[number] extends (x: infer U) => void
? U
: never
This type does what its name suggests. Now, let's address the issue at hand.
type A = {
A: { name: "Foo" }
}
type B = {
B: { name: "Bar" }
}
type Collection<T extends any[]> = TupleToIntersection<{ [I in keyof T]: T[I] }>
declare const C: Collection<[A, B]>
C.A.name // "Foo" as expected
C.B.name // "Bar" as expected
declare const D: Collection<[A, any]>
D // <=== now of any type since an intersection of any and type A results in any
D.A.name // <=== While technically valid, there is no "intelliSense" support here
Is there a way to accomplish this?
One approach could involve keeping the type as "any" and using a typeguard to coerce it into a known shape where needed in code. This aligns with maintaining "typesafe" code according to TypeScript practices, but it may entail defining unnecessary typeguards and types manually instead of relying on automatic system definitions.