# 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. - For an example of using the Vertex AI API, see [Quickstart using the Vertex AI API](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/api-quickstart). - To view this sample in the Cloud console: [Go to Google Cloud console](https://console.cloud.google.com/vertex-ai/generative/language/prompt-examples/Typescript%20migration) <br /> ||
| |---| | **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 { constructor(name, price, category, quantity, manufacturer, sku, discount = 0) { this._name = name; this._price = price; this._category = category; this._quantity = quantity; this._manufacturer = manufacturer; this._sku = sku; this._discount = discount; } get name() { return this._name; } get price() { return this._price; } get category() { return this._category; } get quantity() { return this._quantity; } get manufacturer() { return this._manufacturer; } get sku() { return this._sku; } get discount() { return this._discount; } set name(newName) { this._name = newName; } set price(newPrice) { validateNonNegative(newPrice, 'Price'); this._price = newPrice; } set category(newCategory) { this._category = newCategory; } set quantity(newQuantity) { validateNonNegative(newQuantity, 'Quantity'); this._quantity = newQuantity; } set manufacturer(newManufacturer) { this._manufacturer = newManufacturer; } set sku(newSku) { this._sku = newSku; } set discount(newDiscount) { validateDiscount(newDiscount); this._discount = newDiscount; } getProductDetails() { 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 IProduct { name: string; price: number; category: string; quantity: number; manufacturer: string; sku: string; discount: number; getProductDetails(): string; } class Product implements IProduct { 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-2.5-pro | | **Temperature:** | 0.2 | | **Max output tokens:** | 65535 | | **TopK:** | 64 | | **TopP:** | 0.95 | |