I have this code, I'm interested in whether it is possible to get the name of the passed object inside the useConf function, namely "container__wrapper", and not "Object".
const conf = {
container__wrapper : {
style : {
width : '100%'
}
} as Component,
toolbar : {
ui: "default"
} as Component,
};
const useConf = (config: Object) => {
return config.constructor.name; // Return "Object"
}
console.log(
useConf(conf.container__wrapper);
);
You can't, but you can add a name property to your object for this purpose,
e.g:
const conf = {
container__wrapper : {
name: 'container__wrapper',
style : {
width : '100%'
}
} as Component,
toolbar : {
name: 'toolbar',
ui: "default"
} as Component,
};
const useConf = (config: Object) => {
return config.name; // Returns "container__wrapper"
}
console.log(
useConf(conf.container__wrapper);
);
Like this:
const useConf = (config: Object) => {
return Object.keys(config)[0] // Return "first key name"
}