Search code examples
typescripttypesstructural-typing

TypeScript interface to catch objects with any amount of values


I have an object like this:

{
    "0001": "a",
    "0002": "b",
    "0003": "c",
    ...
}

Can I write a TypeScript interface, which describes this type?


Solution

  • @NitzanTomer's comment is totally valid: interface Map { [key: string]: string }.

    You can also define it as a literal, or as just strings:

    interface LiteralInterface { 
        "0001": "a",
        "0002": "b",
        "0003": "c",
         ...
    }
    
    interface StringInterface { 
        "0001": string,
        "0002": string,
        "0003": string,
         ...
    }
    

    but here you do have to specify each and every property...