0

I have an object like this, lets call theObject variable

         {
            "activate": {
                "from": "pending",
                "to": "active"
            },
            "deactivate": {
                "from": ["pending", "active"],
                "to": "inactive"
            }
        };

and example code like this

class Test {
   constructor(data : Object)
}

const test = new Test(theObject);

The code isn't returning any error since the type is correct. But I want more deep type interface for theObject. The list of objects within theObject can be anything and more, like this for example :

{
            "darkKnight": { // the key should be in string
                "from": "bruceWayne", // from should be string or array of string
                "to": "batman" // to should be string
            },
            "theAmazing": {
                "from": ["peterParker", "milesMorales"],
                "to": "spiderMan"
            },
            "manOfSteel": {
                "from": "clackKent",
                "to": "superman"
            }
        };

The question is, how to write the more deep type definition interface for theObject, rather than using Object type

1

1 Answer 1

1

Based on your samples a fitting interface could look somewhat like this:

interface ObjectType {
    [key: string]: { from: string | string[]; to: string; }
}

Playground

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

1 Comment

I thought that interface only applicable if the object only contain one key. Thank you.

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.