Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript file reading in the wrong variable type

In my spec.ts file it reads this error "Argument of type ‘string’ is not assignable to parameter of type ‘number’." for the value unitPrice, which doesn’t make sense because in my .ts file unitPrice is the type number. These properties are supposed to store the values coming from my api made using springboot and unitPrice is assigned as BigDecimal in my api. So I don’t know where it’s getting that unitPrice is an "Argument of type ‘string’".

This is my .spec.ts file:

import { Product } from './product';

describe('Product', () => {
  it('should create an instance', () => {
    expect(new Product('sku', 'name', 'description', 'unitPrice', 'imageUrl', 'active', 'unitsInStock', 'dateCreated', 'lastUpdated')).toBeTruthy();
  });
});

This is my .ts file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export class Product {
    constructor(public sku: string,
                public name: string,
                public description: string,
                public unitPrice: number,
                public imageUrl: string,
                public active: boolean,
                public unitsInStock: number,
                public dateCreated: Date,
                public lastUpdated: Date
    ) {
        
    }
}

This is a snippet of my json from the api

"_embedded" : {
    "products" : [ {
      "sku" : "BOOK-TECH-1000",
      "name" : "JavaScript - The Fun Parts",
      "description" : "Learn JavaScript",
      "price" : 19.99,
      "imageUrl" : "assets/images/products/placeholder.png",
      "active" : true,
      "unitsInStock" : 100,
      "dateCreated" : "2024-12-19T14:44:23.000+00:00",
      "lastUpdated" : null,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/products/1"
        },
        "product" : {
          "href" : "http://localhost:8080/api/products/1"
        },
        "category" : {
          "href" : "http://localhost:8080/api/products/1/category"
        }
      }
    }

>Solution :

If the unitPrice is of type number, just pass a number value (1) instead of a string ('unitPrice').

Same logic to be applied for active, dateCreated and lastUpdated.

import { Product } from './product';

describe('Product', () => {
  it('should create an instance', () => {
    expect(new Product('sku', 'name', 'description', 1, 'imageUrl', true, 'unitsInStock', new Date(), new Date())).toBeTruthy();
  });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading