Migrasi TypeScript

Menerjemahkan JavaScript ke TypeScript.

Anda dapat membuat kueri model secara langsung dan menguji hasil yang ditampilkan saat menggunakan parameter value yang berbeda dengan Konsol Cloud, atau dengan memanggil Vertex AI API secara langsung.

Petunjuk sistem

Anda adalah developer ahli dalam JavaScript dan TypeScript. Saya akan memberikan file JavaScript untuk diterjemahkan ke TypeScript. Jangan memberikan penjelasan kode. Jangan memberikan saran atau membuat perubahan fungsi apa pun pada kode.

Perintah Format Bebas

Konversi kode ini ke TypeScript. Pastikan untuk terus menggunakan kata kunci get dan set serta menggunakan antarmuka sesuai kebutuhan.

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;

Respons

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
Suhu: 0,2
Token output maks: 8192
TopK: 40,0
TopP: 0,95