skip to Main Content

I’m reasonably new to typescript and I have been attempting to create a DAO for DynamoDB to insert objects into a table. However, when using the spread operator it appears to cause the object to be split into individual characters instead.

My method simply takes the object, adds a few fields to it via the spread operator and then inserts it into the table. From my understanding this should work however as you can see by the console.log() before and after the spread operation on the ‘itemToInsert’ something is going wrong.

The Method

async createItem(item: FleetAccount): Promise<FleetAccount> {
        console.log(item);

        const itemToInsert: FleetAccount = {
            ...item,
            id: generateUniqueKey("ACC"),
            createdAt: new Date().toISOString(),
            updatedAt: new Date().toISOString(),

        };

        console.log(itemToInsert);

        try {
            await this.docClient.send(new PutCommand({
                TableName: this.tableName,
                Item: itemToInsert,
                ConditionExpression: 'attribute_not_exists(id)'
            }));
        } catch (e) {
            if (e instanceof ConditionalCheckFailedException) { //Error when the item already exists.
                return Promise.reject("Item with specified Id already exists.");
            } else {
                console.log("FAILURE!");
                console.log(e);
                return Promise.reject();
            }
        }

        return itemToInsert;
    }

The Interface/Type

export default interface FleetAccount {
    id: string;
    businessName:
    businessAddress: string;
    abn: string;
    primaryContactId: string;
    invoiceIds: Array<string>;
    availableMerchants: Array<string>;
    requireVehicleRegistration: boolean; 
    users: Array<string>;
    createdAt:
    updatedAt: string;
}

Item

{
"businessName": "Test Company",
"businessAddress": "123 Fake Street, Canberra, Australia, 2903",
"abn": "123456",
"primaryContactId": "FUI1234",
"invoiceIds": ["1"],
"availableMerchants": ["1"],
"requireVehicleRegistration": false,
"users": ["1"]
}

itemToInsert

{
  '0': '{',
  '1': 'r',
  '2': 'n',
  '3': ' ',
  '4': ' ',
  '5': ' ',
  '6': ' ',
  '7': '"',
  '8': 'b',
  '9': 'u',
  '10': 's',
  '11': 'i',
  '12': 'n',
  '13': 'e',
  '14': 's',
  '15': 's',
  '16': 'N',
  '17': 'a',
  '18': 'm',
  '19': 'e',
  '20': '"',
  '21': ':',
  '22': ' ',
  '23': '"',
  '24': 'T',
  '25': 'e',
  '26': 's',
  '27': 't',
  '28': ' ',
  '29': 'C',
  '30': 'o',
  '31': 'm',
  '32': 'p',
  '33': 'a',
  '34': 'n',
  '35': 'y',
  '36': '"',
  '37': ',',
  '38': 'r',
  '39': 'n',
  '40': ' ',
  '41': ' ',
  '42': ' ',
  '43': ' ',
  '44': '"',
  '45': 'b',
  '46': 'u',
  '47': 's',
  '48': 'i',
  '49': 'n',
  '50': 'e',
  '51': 's',
  '52': 's',
  '53': 'A',
  '54': 'd',
  '55': 'd',
  '56': 'r',
  '57': 'e',
  '58': 's',
  '59': 's',
  '60': '"',
  '61': ':',
  '62': ' ',
  '63': '"',
  '64': '1',
  '65': '2',
  '66': '3',
  '67': ' ',
  '68': 'F',
  '69': 'a',
  '70': 'k',
  '71': 'e',
  '72': ' ',
  '73': 'S',
  '74': 't',
  '75': 'r',
  '76': 'e',
  '77': 'e',
  '78': 't',
  '79': ',',
  '80': ' ',
  '81': 'C',
  '82': 'a',
  '83': 'n',
  '84': 'b',
  '85': 'e',
  '86': 'r',
  '87': 'r',
  '88': 'a',
  '89': ',',
  '90': ' ',
  '91': 'A',
  '92': 'u',
  '93': 's',
  '94': 't',
  '95': 'r',
  '96': 'a',
  '97': 'l',
  '98': 'i',
  '99': 'a',
  '100': ',',
  '101': ' ',
  '102': '2',
  '103': '9',
  '104': '0',
  '105': '3',
  '106': '"',
  '107': ',',
  '108': 'r',
  '109': 'n',
  '110': ' ',
  '111': ' ',
  '112': ' ',
  '113': ' ',
  '114': '"',
  '115': 'a',
  '116': 'b',
  '117': 'n',
  '118': '"',
  '119': ':',
  '120': ' ',
  '121': '"',
  '122': '1',
  '123': '2',
  '124': '3',
  '125': '4',
  '126': '5',
  '127': '6',
  '128': '"',
  '129': ',',
  '130': 'r',
  '131': 'n',
  '132': ' ',
  '133': ' ',
  '134': ' ',
  '135': ' ',
  '136': '"',
  '137': 'p',
  '138': 'r',
  '139': 'i',
  '140': 'm',
  '141': 'a',
  '142': 'r',
  '143': 'y',
  '144': 'C',
  '145': 'o',
  '146': 'n',
  '147': 't',
  '148': 'a',
  '149': 'c',
  '150': 't',
  '151': 'I',
  '152': 'd',
  '153': '"',
  '154': ':',
  '155': ' ',
  '156': '"',
  '157': 'F',
  '158': 'U',
  '159': 'I',
  '160': '1',
  '161': '2',
  '162': '3',
  '163': '4',
  '164': '"',
  '165': ',',
  '166': 'r',
  '167': 'n',
  '168': ' ',
  '169': ' ',
  '170': ' ',
  '171': ' ',
  '172': '"',
  '173': 'i',
  '174': 'n',
  '175': 'v',
  '176': 'o',
  '177': 'i',
  '178': 'c',
  '179': 'e',
  '180': 'I',
  '181': 'd',
  '182': 's',
  '183': '"',
  '184': ':',
  '185': ' ',
  '186': '[',
  '187': '"',
  '188': '1',
  '189': '"',
  '190': ']',
  '191': ',',
  '192': 'r',
  '193': 'n',
  '194': ' ',
  '195': ' ',
  '196': ' ',
  '197': ' ',
  '198': '"',
  '199': 'a',
  '200': 'v',
  '201': 'a',
  '202': 'i',
  '203': 'l',
  '204': 'a',
  '205': 'b',
  '206': 'l',
  '207': 'e',
  '208': 'M',
  '209': 'e',
  '210': 'r',
  '211': 'c',
  '212': 'h',
  '213': 'a',
  '214': 'n',
  '215': 't',
  '216': 's',
  '217': '"',
  '218': ':',
  '219': ' ',
  '220': '[',
  '221': '"',
  '222': '1',
  '223': '"',
  '224': ']',
  '225': ',',
  '226': 'r',
  '227': 'n',
  '228': ' ',
  '229': ' ',
  '230': ' ',
  '231': ' ',
  '232': '"',
  '233': 'r',
  '234': 'e',
  '235': 'q',
  '236': 'u',
  '237': 'i',
  '238': 'r',
  '239': 'e',
  '240': 'V',
  '241': 'e',
  '242': 'h',
  '243': 'i',
  '244': 'c',
  '245': 'l',
  '246': 'e',
  '247': 'R',
  '248': 'e',
  '249': 'g',
  '250': 'i',
  '251': 's',
  '252': 't',
  '253': 'r',
  '254': 'a',
  '255': 't',
  '256': 'i',
  '257': 'o',
  '258': 'n',
  '259': '"',
  '260': ':',
  '261': ' ',
  '262': 'f',
  '263': 'a',
  '264': 'l',
  '265': 's',
  '266': 'e',
  '267': ',',
  '268': 'r',
  '269': 'n',
  '270': ' ',
  '271': ' ',
  '272': ' ',
  '273': ' ',
  '274': '"',
  '275': 'u',
  '276': 's',
  '277': 'e',
  '278': 'r',
  '279': 's',
  '280': '"',
  '281': ':',
  '282': ' ',
  '283': '[',
  '284': '"',
  '285': '1',
  '286': '"',
  '287': ']',
  '288': 'r',
  '289': 'n',
  '290': '}',
  id: 'ACCBPNEJB84SMJJHFA',
  createdAt: '2023-11-18T14:24:38.256Z',
  updatedAt: '2023-11-18T14:24:38.256Z'
}

Is there another way that these fields should be added to this object to make it more clear to JS that this is not a string? I have already tried Object.assign() with the same result.

2

Answers


  1. That happens if you spread a string.

    For example, take this piece of code,

    const myObject = {
        ..."Random String",
    }
    
    console.log(myObject)
    

    The output is

    {
      "0": "R",
      "1": "a",
      "2": "n",
      "3": "d",
      "4": "o",
      "5": "m",
      "6": " ",
      "7": "S",
      "8": "t",
      "9": "r",
      "10": "i",
      "11": "n",
      "12": "g"
    }
    

    Can you verify that the value in the variable item of type FleetAccount is actually FleetAccount and not a String?

    You could do something like and see if it prints string.

    console.log(typeof(item))
    

    The variable item here

    async createItem(item: FleetAccount): Promise<FleetAccount> {
      ...
    }
    

    could hold a string value in Typescript even though you explicitly state it to be of type FleetAccount, since Typescript is just a compile time checker for Javascript which does not have the concept of types, ie, types are not enforced at runtime.

    Login or Signup to reply.
  2. Looks like working for me link to check playground

    let createItem = (item: Object) =>
    {
        const itemToInsert =
        {
            ...item,
            createdAt: new Date().toISOString(),
            updatedAt: new Date().toISOString(),
    
        };
    
        return itemToInsert;
    }
    
    let globalItem =
    {
      "businessName": "Test Company",
      "businessAddress": "123 Fake Street, Canberra, Australia, 2903",
      "abn": "123456",
      "primaryContactId": "FUI1234",
      "invoiceIds": ["1"],
      "availableMerchants": ["1"],
      "requireVehicleRegistration": false,
      "users": ["1"]
    };
    
    interface GlobalItemConfig
    {
        id: string;
        businessName: any;
        businessAddress: string;
        abn: string;
        primaryContactId: string;
        invoiceIds: Array<string>;
        availableMerchants: Array<string>;
        requireVehicleRegistration: boolean; 
        users: Array<string>;
        createdAt: string;
        updatedAt: string;
    };
    
    let computedItem: GlobalItemConfig;
    
    computedItem = createItem(globalItem) as GlobalItemConfig;
    
    console.log(computedItem);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search