Hello World
const message: string = "Hello, world!";
console.log(message);
Run with a TypeScript runtime or compile first:
npx tsx main.ts
This defines a typed string and logs it. It is the basic TypeScript shape for a tiny script.
Types
type User = {
id: number;
name: string;
active: boolean;
};
const user: User = {
id: 1,
name: "Dan",
active: true,
};
This type describes the expected object shape, then the object literal follows that contract.
Functions
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("world"));
This function makes both the input and output types explicit, which is one of the main benefits of TypeScript.