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 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
Download from the command line
curl -L -o sample.ts https://files.samplefiles.com/code/ts/sample.tswget https://files.samplefiles.com/code/ts/sample.tshttps://files.samplefiles.com/code/ts/sample.tsprintf '%s %s\n' c35f84772d8cedaa79c5abc5771d11129af1ccac437735d2fe9bff89ce291aac sample.ts | shasum -a 256 -cOr check the download in your browser with the checksum calculator.