Code · 1 file

Sample TypeScript File

TypeScript adds static types to JavaScript. The .ts extension is shared with MPEG transport streams, so these files are served as text/plain. The TypeScript sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.

TypeScript · 733 B

Sample TypeScript file

An idiomatic TypeScript example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.

CC0 · free for any use · Verified by an independent tool · Report a problem

sample.ts — first linesTypeScript
// Sample TypeScript module — samplefiles.com
export interface Customer {
  readonly id: number;
  name: string;
  balance: number;
  tags?: string[];
}

export const isVip = (c: Customer): boolean => c.balance >= 10_000;

export function totalBalance(customers: readonly Customer[]): number {
  return customers.reduce((sum, c) => sum + c.balance, 0);
}

const customers: Customer[] = [
  { id: 1, name: 'Ada', balance: 12_500 },
  { id: 2, name: 'Grace', balance: 830.5, tags: ['new'] },
  { id: 3, name: 'Linus', balance: 10_000 },
];

for (const c of customers) {
  console.log(`${c.name.padEnd(6)} ${c.balance.toFixed(2).padStart(10)} ${isVip(c) ? 'VIP' : ''}`);
}
console.log(`Total: ${totalBalance(customers).toFixed(2)}`);

File details

Size
733 B (733 bytes)
MIME type
text/plain
SHA-256
c35f84772d8cedaa79c5abc5771d11129af1ccac437735d2fe9bff89ce291aac
MD5
c8ff6f9f8c4bb860595e4704c7ae37f2

Download from the command line

curl -L -o sample.ts https://files.samplefiles.com/code/ts/sample.ts

Or check the download in your browser with the checksum calculator.