Foundations
Hello TypeScript
Start with a small TypeScript program that stores a typed value and prints it.
type annotation
TypeScript lets you write JavaScript plus explicit types such as `string`, so tools can catch mistakes before the code runs.
Hello
hello.ts
Replay: real traced execution (multi-file project)
const name: string = "TypeScript";
const message: string = `Hello, ${name}!`;
console.log(message);
const name: string = "Ada";
const message: string = `Hello, ${name}!`;
console.log(message);
const name: string = "Lattice";
const message: string = `Hello, ${name}!`;
console.log(message);
name ← TypeScript, message ← Hello, TypeScript!
1const name→ TypeScript: string = "TypeScript"; //@name="Ada", "Lattice"2const message→ Hello, TypeScript!: string = `Hello, ${nameTypeScript}!`;34console.log(messageHello, TypeScript!);outputHello, TypeScript!
name ← Ada, message ← Hello, Ada!
1const name→ Ada: string = "Ada";2const message→ Hello, Ada!: string = `Hello, ${nameAda}!`;34console.log(messageHello, Ada!);outputHello, Ada!
name ← Lattice, message ← Hello, Lattice!
1const name→ Lattice: string = "Lattice";2const message→ Hello, Lattice!: string = `Hello, ${nameLattice}!`;34console.log(messageHello, Lattice!);outputHello, Lattice!
Follow the Message
namestarts as"TypeScript".- The template text is
Hello, ${name}!. messagebecomesHello, TypeScript!.console.log(message)printsHello, TypeScript!. | name | message | | --- | --- | | TypeScript | Hello, TypeScript! | | Ada | Hello, Ada! | | Lattice | Hello, Lattice! |
Exercise: hello.ts
Reproduce Hello, TypeScript!, then try Ada and Lattice and predict each greeting before running it.