Some libraries come with a very complex type structure which beats the purpose of the usage of Typescipt as there is way too much information in the type and is being truncated or is too nested to be usable. Extending such libraries can therefore be a total hit & miss situation. I tend to spend way too much time understanding and resolving the types that I may want to use.
The main question
I was wondering whether there's a way to actually resolve the result type the way that typescript internally does it for itself so that code hinting/intellisense works etc.
A simplified (still easily comprehensible) example:
const sizes = ['s', 'm', 'l', 'xl'];
const materials = ['cotton', 'linen', 'elastan', 'goretex'];
type Continent = 'North America' | 'South America' | 'Europe' | 'Asia' | 'Australia' | 'Africa';
interface Address {
street: string;
post: string;
country: string;
continent?: Continent;
}
interface Company extends Partial<Address> {
name: string;
}
interface Garmet {
name: string;
material: typeof materials[number];
size: typeof sizes[number];
manufacturer?: Exclude<Company, 'continent'>;
}
So what I would like to end up with is a completely resolved drilled-down type:
interface Garmet {
name: string;
material: 'cotton' | 'linen' | 'elastan' | 'goretex';
size: 's' | 'm' | 'l' | 'xl';
manufacturer?: {
name: string;
street?: string;
post?: string;
country?: string;
}
}
The generics may go very deep and provide all kind of type inferences, keyofs, typeofs etc... I'm currently working with MUI material and found that writing completely custom components that can be themed in the same way as the OOTB components is a very lousy experience.
Note: I could of course unwind the types myself by replacing the types until I get the whole type tree structure to the very leaves, but that would be very time consuming. There must be a programmatic way to do the same.