0

Let's say I have function which takes this object as one of the arguments:

{
    section: "main",
    sectionColor: "#0072c6",
    favIcon: "default",
    libraries: {
        value: "librariesFiles",
        type: "jsIncludes"
    },
    components: {
        value: "comoponentFiles",
        type: "jsIncludes"
    }
}

Here is interface I came up to define this property in function:

interface IgenerateFileArgumentObjectItem {
    value: string;

    type: string;
}

export interface IgenerateFileArgument {
    [key: string]: (string | IgenerateFileArgumentObjectItem);
}

Shortly, I'm trying define object with dynamic number of properties which can be equal either to string value either to another object with two properties...

But compiler is complaining, and I think issue is in this line: (string | IgenerateFileArgumentObjectItem)

Is there any obvious mistake or am I trying to do impossible thing?

I'm getting error here

And the error

4
  • What are librariesFiles and comoponentFiles are they strings? Commented Aug 21, 2016 at 19:44
  • What exactly is the compiler complaining about? It compiles for me without errors. Commented Aug 21, 2016 at 19:52
  • Nitzan, yes those are strings. I have updated question with some screenshots Commented Aug 21, 2016 at 20:20
  • 1
    Screenshots are not as good as text/code, plus from the looks of it, it seems that no, librariesFiles and comoponentFiles are not strings, they are string[], am I right? Are there any more things that you can add/fix about the question? Commented Aug 21, 2016 at 20:49

1 Answer 1

1

I have created the following example, you can view it here in the TS playground. I was able to reconstruct you're issue. It seems that either one (or all) of the following librariesFiles, componentFiles, modellingContentStates, sharePointStates is of type string[].

interface IgenerateFileArgumentObjectItem {
    value: string;

    type: string;
}

export interface IgenerateFileArgument {
    [key: string]: (string | IgenerateFileArgumentObjectItem);
}

var p: string = 'uo'
var lol :string[] = ['no', 'po']; // Fix this to string type
var a: IgenerateFileArgument = {
    c: {
        value: p,
        type:'mo'
    },
    b:'lol',
    e: {
        value:'yo',
        type:'mo'
    },
    m: {  // you can remove this instead
        value:lol,
        type:'go'
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Bingo! Thank you, silly me missed something. Also when working between different types, good thing to know about type casting

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.