📘
NavDoc by Bash School
GithubContact
📘
NavDoc by Bash School
  • 🎓Introduction
  • 🐢Getting Started
  • ⚡Changelog
  • 👨‍🚀Maintainers
  • 🛣️Roadmap
  • Fundamentals
    • The Internet
      • Introduction
      • What is a URL
      • What is a port
      • The DNS protocol
      • The TCP protocol
      • The UDP protocol
      • The Web
      • The HTTP protocol
      • Hyperlinks
      • What is a Web browser
      • What is a Web server
    • HTML
      • Your first HTML page
      • Text tags
      • Attributes
      • Links
      • Images
      • Lists
      • Head Tags
      • Container tags
    • CSS
      • Introduction
      • Colors
      • selectors
      • Cascade
      • Specificity
      • Units
      • Advanced selectors
      • Typography
      • The box model
      • The display property
      • Responsive design
  • JavaScript
    • Basics
      • Introduction
      • Literals , Identifiers, Variables
      • Comments
      • The difference between let, const and var
      • Types
      • Operators and expressions
      • Arithmetic operators
      • The assignment operator
      • Operators precedence
      • Strings
      • Numbers
      • Semicolons, white space and sensitivity
      • Arrays
      • Conditionals
      • Loops
      • Functions
      • Objects
      • Arrays + functions
      • OOPS
      • Asynchronous
      • Scope, hoisting, event loop
      • ES Modules
      • Errors and exceptions
      • Built-in objects
        • The global object
        • Object properties
        • Number
        • String
        • Math
        • JSON
        • Date
        • Intl
        • Set and Map
      • More operators
    • Nodejs
      • Getting Started
      • Installation
      • Hello World in Node
      • Modules
      • Packages
      • File Handling
      • HTTP Request
      • Processing Files
      • HTTP
    • Express.js
      • Getting Started
      • Middleware
      • Serve Static Assets
      • How to Send Files to the Client
      • Sessions
      • Validate Input
      • Sanitizing Data
      • Forms
      • File Uploads
    • React
      • Setting up a React project with Vite
      • React Components
      • Introduction to JSX
      • Using JSX to compose UI
      • The difference between JSX and HTML
      • Embedding JavaScript in JSX
      • Handling user events
      • Managing state
      • Component props
      • Data flow
      • Lifecycle events
      • Managing forms in React
      • Install the React Developer Tools
      • Installing Tailwind CSS in a React app
      • Build a counter in React
    • TypeScript
      • Key Benefits
      • Types of Languages
      • The Need for TypeScript
      • What is TypeScript?
      • The tsc Compiler
      • Basic Types in TypeScript
      • tsconfig
      • Interfaces
      • Types
      • Arrays in TypeScript
      • Enums
      • Exporting and importing
    • MongoDB
      • SQL vs. NoSQL Databases
      • Installing MongoDB
      • MongoDB Databases and Collections
      • Working with Documents
      • MongoDB Operators
      • Sorting, Indexing & Searching
      • Built-in Methods
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. JavaScript
  2. TypeScript

Basic Types in TypeScript

TypeScript provides several basic types that are essential for building robust applications. These include number, string, boolean, null, and undefined.

Let's create some simple applications to understand how to use these types effectively.

Problem 1 - Hello World

Thing to Learn: How to give types to function arguments.

Task: Write a function that greets a user given their first name.

  • Argument: firstName (string)

  • Logs: "Hello {firstName}"

  • Return: None

Solution:

function greetUser(firstName: string): void {
    console.log(`Hello ${firstName}`);
}

// Example usage:
greetUser('Alice');

Problem 2 - Sum Function

Thing to Learn: How to assign a return type to a function.

Task: Write a function that calculates the sum of two numbers.

  • Arguments: a (number), b (number)

  • Returns: Sum of a and b (number)

Solution:

function sum(a: number, b: number): number {
    return a + b;
}

// Example usage:
console.log(sum(5, 7));  // Output: 12

Problem 3 - Return True or False Based on Age

Thing to Learn: Type inference.

Task: Write a function that returns true if a user is 18 or older.

  • Function Name: isLegal

  • Argument: age (number)

  • Returns: true if age is 18 or older, otherwise false (boolean)

Solution:

function isLegal(age: number): boolean {
    return age >= 18;
}

// Example usage:
console.log(isLegal(20));  // Output: true
console.log(isLegal(17));  // Output: false

Problem 4 - Run a Function After 1 Second

Thing to Learn: How to handle functions as arguments.

Task: Create a function that takes another function as input and runs it after 1 second.

  • Function Name: runAfterOneSecond

  • Argument: fn (function)

  • Returns: None

Solution:

function runAfterOneSecond(fn: () => void): void {
    setTimeout(fn, 1000);
}

// Example usage:
runAfterOneSecond(() => {
    console.log('This runs after 1 second');
});

These examples illustrate the use of TypeScript's basic types and provide a foundation for understanding how to work with typed arguments, return types, and type inference in functions.

PreviousThe tsc CompilerNexttsconfig

Last updated 12 months ago

Was this helpful?