Code Logo

Type Alias Person Formatter

Published at24 Jul 2026
TypeScript Functions Easy 0 views
Like0

Write a TypeScript function that uses a type alias. Define a type alias Person = { name: string; age: number }. Write a function that takes a Person and returns a formatted string.

Type aliases allow you to define reusable type definitions. They can represent object shapes, union types, or any other type. Unlike interfaces, type aliases can represent primitive types, unions, and tuples.

Type aliases are defined with the type keyword and are a fundamental TypeScript feature for organizing types. They improve code readability by giving meaningful names to complex types.

Type aliases improve code maintainability by centralizing type definitions. When the type changes, only the alias needs updating rather than every function signature. They are particularly useful in large codebases where the same object shape appears in many function signatures.

Type aliases improve code maintainability by centralizing type definitions. When the type changes, only the alias needs updating rather than every function signature. They are particularly useful in large codebases where the same object shape appears in many function signatures.

The type keyword in TypeScript can define object shapes, union types, intersection types, tuple types, and more. Unlike interfaces, type aliases cannot be extended but can represent any type including unions like string | number and primitives like string.

Type aliases are evaluated lazily and can reference themselves, enabling recursive type definitions for data structures like JSON trees or linked lists.

Example Input & Output

Example 1
Input
{"name":"Bob","age":25}
Output
"Bob is 25 years old"
Example 2
Input
{"name":"Alice","age":30}
Output
"Alice is 30 years old"

Algorithm Flow

Recommendation Algorithm Flow for Type Alias Person Formatter
Recommendation Algorithm Flow for Type Alias Person Formatter

Solution Approach

Write a TypeScript function that uses a type alias. Define a type alias Person = { name: string; age: number }. Write a function that takes a Person and returns a formatted string.

Type aliases allow you to define reusable type definitions. They can represent object shapes, union types, or any other type. Unlike interfaces, type aliases can represent primitive types, unions, and tuples.

Type aliases are defined with the type keyword and are a fundamental TypeScript feature for organizing types. They improve code readability by giving meaningful names to complex types.

Best Answers

typescript - Approach 1
function formatPerson(p: {name: string; age: number}): string {
    return p.name + ' is ' + p.age + ' years old';
}