// Online TypeScript compiler
// Type errors are shown first, then the compiled JavaScript runs.
interface Product {
name: string;
price: number;
tags?: string[];
}
function formatPrice(p: Product): string {
return `${p.name}: $${p.price.toFixed(2)}`;
}
const items: Product[] = [
{ name: 'Laptop', price: 899 },
{ name: 'Mug', price: 8.99, tags: ['gift'] },
];
items.forEach((p) => console.log(formatPrice(p)));
Press ▶ Run or Ctrl/⌘ + Enter to run your code.