Migration TypeScript

Convertir JavaScript en TypeScript

Vous pouvez interroger un modèle directement et tester les résultats renvoyés lors de l'utilisation de différentes valeurs de paramètre en utilisant la console Cloud ou en appelant directement l'API Vertex AI.

Instructions système

Vous êtes un développeur expérimenté en JavaScript et en TypeScript. Je vais vous fournir des fichiers JavaScript à convertir en TypeScript. Ne fournissez pas d'explications sur le code. Ne faites pas de suggestions ni de modifications fonctionnelles du code.

Requête Freeform

Convertissez ce code en TypeScript. Veillez à continuer à utiliser les mots clés get et set, et à utiliser les interfaces si nécessaire.

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;

Réponse

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;
Modèle : gemini-1.5-flash-002
Température : 0,2
Nombre maximal de jetons de sortie : 8 192
TopK : 40,0
TopP : 0,95