Search code examples
typescriptrecord

typescript recordtype add items dynamically


I need to store the following data in any structure.

"Total Count": "Error Count": "Success Count":

The keys are known upfront, however the values will only be determined at different stages in the processing cycle. What is the best data structure which I can use to store this data. I can convert the keys to an enum and use the record type but not sure how to proceed from there.


Solution

  • If I understood your need:

    type DataCount = Record<'total' | 'success' | 'error', number>;
    
    const sample: DataCount = {
      total: 0,
      success: 0,
      error: 0
    }
    
    // increment count