TypeScript migration

Translate JavaScript to TypeScript.

You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly.

System instructions

You are an expert developer in both Javascript and TypeScript. I will give you Javascript files to be translated to TypeScript. Don't give explanations of the code. Don't give suggestions or make any functional changes to the code.

Prompt Freeform

Convert this code to TypeScript. Make sure to continue using get and set keywords and use interfaces as needed.

import { validateNonNegative, validateDiscount } from './validationHelpers';

class Product {
  private _name: string;
  private _price: number;
  private _category: string;
  private _quantity: number;
  private _manufacturer: string;
  private _sku: string;
  private _discount: number;

  constructor(name: string, price: number, category: string, quantity: number, manufacturer: string, sku: string, discount: number = 0) {
    this._name = name;
    this._price = price;
    this._category = category;
    this._quantity = quantity;
    this._manufacturer = manufacturer;
    this._sku = sku;
    this._discount = discount;
  }

  get name(): string { return this._name; }
  get price(): number { return this._price; }
  get category(): string { return this._category; }
  get quantity(): number { return this._quantity; }
  get manufacturer(): string { return this._manufacturer; }
  get sku(): string { return this._sku; }
  get discount(): number { return this._discount; }

  set name(newName: string) { this._name = newName; }
  set price(newPrice: number) { 
    validateNonNegative(newPrice, 'Price');
    this._price = newPrice;
  }
  set category(newCategory: string) { this._category = newCategory; }
  set quantity(newQuantity: number) { 
    validateNonNegative(newQuantity, 'Quantity');
    this._quantity = newQuantity;
  }
  set manufacturer(newManufacturer: string) { this._manufacturer = newManufacturer; }
  set sku(newSku: string) { this._sku = newSku; }
  set discount(newDiscount: number) { 
    validateDiscount(newDiscount);
    this._discount = newDiscount;
  }

  getProductDetails(): string {
    const discountedPrice = this._price * (1 - this._discount / 100);
    return `Product: ${this._name}, Price: $${discountedPrice.toFixed(2)}, Category: ${this._category}, Quantity: ${this._quantity}, Manufacturer: ${this._manufacturer}, SKU: ${this._sku}, Discount: ${this._discount}%`;
  }
}

export default Product;

Response

import { validateNonNegative, validateDiscount } from './validationHelpers';

interface ProductInterface {
  name: string;
  price: number;
  category: string;
  quantity: number;
  manufacturer: string;
  sku: string;
  discount: number;
  getProductDetails(): string;
}

class Product implements ProductInterface {
  private _name: string;
  private _price: number;
  private _category: string;
  private _quantity: number;
  private _manufacturer: string;
  private _sku: string;
  private _discount: number;

  constructor(name: string, price: number, category: string, quantity: number, manufacturer: string, sku: string, discount: number = 0) {
    this._name = name;
    this._price = price;
    this._category = category;
    this._quantity = quantity;
    this._manufacturer = manufacturer;
    this._sku = sku;
    this._discount = discount;
  }

  get name(): string { return this._name; }
  get price(): number { return this._price; }
  get category(): string { return this._category; }
  get quantity(): number { return this._quantity; }
  get manufacturer(): string { return this._manufacturer; }
  get sku(): string { return this._sku; }
  get discount(): number { return this._discount; }

  set name(newName: string) { this._name = newName; }
  set price(newPrice: number) { 
    validateNonNegative(newPrice, 'Price');
    this._price = newPrice;
  }
  set category(newCategory: string) { this._category = newCategory; }
  set quantity(newQuantity: number) { 
    validateNonNegative(newQuantity, 'Quantity');
    this._quantity = newQuantity;
  }
  set manufacturer(newManufacturer: string) { this._manufacturer = newManufacturer; }
  set sku(newSku: string) { this._sku = newSku; }
  set discount(newDiscount: number) { 
    validateDiscount(newDiscount);
    this._discount = newDiscount;
  }

  getProductDetails(): string {
    const discountedPrice = this._price * (1 - this._discount / 100);
    return `Product: ${this._name}, Price: $${discountedPrice.toFixed(2)}, Category: ${this._category}, Quantity: ${this._quantity}, Manufacturer: ${this._manufacturer}, SKU: ${this._sku}, Discount: ${this._discount}%`;
  }
}

export default Product;
Model: gemini-1.5-flash-002
Temperature: 0.2
Max output tokens: 8192
TopK: 40.0
TopP: 0.95