0

Is it possible to generate the Json object from interfaces populated with the key type in string? See below the interface example:

export interface shop {
    refNo: Number
    domain?: string[],    
    details: {
        name? : string,
        cui : string,
        contact?: {
            name?: string,
            phone?: string
        }[]
    }

This should be the result:

let shopObj = { 
    refNo: "Number"
    domain?: ["string"], // "Array String"   
    details: {
        name? : "string",
        cui : "string",
        contact?: [{
            name?: "string", 
            phone?: "string"
        }]
    }
}

are there any utilities that do this, does anyone have any idea how this transformation can be done from the interface?

2

1 Answer 1

0

What you're trying to do is impossible, since interfaces are types and not values, so you can't treat it as a value..

The only thing you can do, is initiate a variable and then extract the type of each field by using typeof, but its very off from what you're trying to achieve, and its very crooked and quite ugly..

Sign up to request clarification or add additional context in comments.

1 Comment

You can use static code analysis to do what the OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.