so I've been working with react and typescript this year and I noticed how difficult it can sometime be to be able to type some complex objects. therefore, I was wondering if there could be a way or a helper function that we could call with an object and be able to return a typescript interface or a type back which we could then use to write it in our code and use it as a type for whenever we are using the object.
Example: say we have an object as such:
const complexObject = {
"hsl": {
"h": 118.48101265822783,
"s": 1,
"l": 0.5,
"a": 1
},
"hex": "#06ff00",
"rgb": {
"r": 6,
"g": 255,
"b": 0,
"a": 1
},
"hsv": {
"h": 118.48101265822783,
"s": 1,
"v": 1,
"a": 1
},
"oldHue": 118.48101265822784
}
as you can see typing this object could be somewhat troublesome, especially to inexperienced developers like myself. now if there could be a library that we could call in this context getType then we could do something like this:
getType(complexObject);
// return
type complexObject = {
hex: string;
hsl: { h: number; s: number; l: number; a: number };
hsv: { h: number; s: number; v: number; a: number };
rgb: { r: number; g: number; b: number; a: number };
};