angular - Typescript file reading in the wrong variable type - Stack Overflow

admin2025-05-01  1

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

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"
        }
      }
    }

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

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"
        }
      }
    }
Share Improve this question edited Jan 2 at 21:05 Naren Murali 61.3k5 gold badges44 silver badges82 bronze badges asked Jan 2 at 21:00 Jace JohnsonJace Johnson 731 silver badge5 bronze badges 2
  • 1 You are literally passing a string as the unitPrice parameter so it's a bit baffling why you would think that it's a number. – Guy Incognito Commented Jan 2 at 21:05
  • @Guy Incognito I do now realize that my question was dumb. I didn't realize that the spec.ts file is a test file and the variables inside the parentheses I was referring to was for test. – Jace Johnson Commented Jan 2 at 22:19
Add a comment  | 

1 Answer 1

Reset to default 2

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();
  });
});
转载请注明原文地址:http://anycun.com/QandA/1746096810a91620.html