Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:21:08+00:00 2026-06-17T05:21:08+00:00

My TypeScript application makes frequent use of a pattern where I have a set

  • 0

My TypeScript application makes frequent use of a pattern where I have a set of data (an object) that is stored in a string index (another object) for quick lookup by key. Implementing this in TypeScript is easy, but requires two interface definitions (for the data object, and for the string indexer object). I’m finding that my code is becoming littered with these string indexer interfaces, which don’t add much value, and am seeking a way to have more readable / maintainable code.

Is there a way to declare a variable of type string indexer inline, much like we can do with an array (a numeric indexer)? Here’s an example of what I’m doing, and what I would like to do:

interface MyObject {
    foo: string;
    bar: string;
}

// is there a way to not have to define this interface?
interface MyObjectByKey {
    [index: string]: MyObject;
}

class Foo {
    // this works: inline declaration of variable of type numeric indexer
    myObjectsByIndex: MyObject[] = [];

    // this works: variable of type string indexer, but requires extra interface
    myObjectsByKey: MyObjectByKey = {};

    // wish I could do something like this ... (can I?)
    myObjectsByKeyWish: MyObject[index: string] = {};
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:21:09+00:00Added an answer on June 17, 2026 at 5:21 am

    Yes there is:

    class Foo {
        myObjectsByKey: { [index: string]: MyObject; } = {};
    }
    

    this is basically an inline declaration of an interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TypeScript class, with a function that I intend to use as
I am trying to use TypeScript in a Windows 8 app (html5/JS) I have
I have defined the following interface in typescript: interface MyInterface { () : string;
How would I use TypeScript in a Windows 8 Store app that's JavaScript-based? I
I have 2 typescript files. commons.d.ts module commons { interface IUser { name:string; }
I have spent some time reading the Typescript language specification and am somewhat confused
I'm working on a testing application using TypeScript and BackboneJS. It's mainly an employees
I am trying to run the TypeScript compiler from my Java application. To start,
Given the fact that TypeScript is Open Source, is this Web-Based IDE also available
I have a problem that I suppose comes from my ASP.NET MVC project using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.