Home > OS >  Create an object that contains property names of another object
Create an object that contains property names of another object

Time:05-07

I am trying to create a TS validated utility that iterates over the first level properties of the object and returns a new one with its property name.

JS-wise it's quite straightforward, but I would appreciate a bit if you can help me a bit define a correct type structure. I've created a TS Playground link with the code example

P.S: Feel free to drop any suggestion on type refactor if you know a better way :-)

CodePudding user response:

This is absolutely possible and simple with a mapped type:

type PropMap<T> = {
    [K in keyof T]: K;
};

And then we change the definition of the function to return this:

function createObjectKeys<T> (object: T): PropMap<T> {

Unfortunately a cast is required here:

    return keys as PropMap<T>;

But if you try it in the playground below you'll see that it produces the correct types:

Playground

CodePudding user response:

You don't actually need to create any type helper for this, your object itself holds sufficient info to make the code snippet work

/* Types */
type ObjectWithStrings = {
    [index: string]: string
}

type InterfaceRecipeComponents = {
    information: boolean
    ingredients: boolean
    method: boolean
}

/* Logic */
function createObjectKeys<T1> (object: T1): ObjectWithStrings {
    const keys: ObjectWithStrings  =  {}

    Object.keys(object).forEach((propertyName) => {
        keys[propertyName] = propertyName
    })

    return keys
}


const recipeComponents: InterfaceRecipeComponents = {
    information: false,
    ingredients: true,
    method: true
}

export const recipeComponentsKeys = createObjectKeys(recipeComponents)

console.log(recipeComponentsKeys.information)

Code Playground

  • Related