DEV Community

Cover image for Mastering TypeScript by Applying These Rules
Emma Richardson
Emma Richardson

Posted on • Edited on

1

Mastering TypeScript by Applying These Rules

Identifiers:

Identifiers are names assigned to elements like variables, methods, classes, arrays, etc., within a TypeScript program. There are specific rules that must be followed when declaring identifiers:
Identifier Naming:
An identifier can start with either an uppercase or lowercase letter: myVariable, MyClass

  • It cannot begin with a number:
    2variable is invalid.

  • Only two symbols, _ (underscore) and $ (dollar sign), are allowed within an identifier:
    _myVariable, my$Method are valid.

  • Identifiers cannot contain spaces or other special characters:
    my-variable, my#var are invalid.

Case Sensitivity:
Identifiers are case-sensitive, meaning myVariable and MyVariable are considered two distinct identifiers.

No Reserved Keywords:
Identifiers cannot use reserved TypeScript keywords, such as if, else, class, etc., as names.

Examples of Valid and Invalid Identifiers:
• Valid: myVariable, _temp, my$Method, Array1
• Invalid: 2ndValue, my-variable, class, #temp

2. Keywords:
Keywords in TypeScript are predefined words that perform specific actions or represent certain functionalities. These cannot be used as identifiers (variable names, function names, etc.). Some common TypeScript keywords include:
• Control Flow: if, else, switch, case, break
• Data Types: any, number, string, boolean, void
• Class Modifiers: public, private, protected, readonly
• Other: as, get, set, typeof, instanceof, module, type

3. Variable Declaration:
Variables can be declared using let, const, or var (though var is generally discouraged due to scoping issues).

let age: number = 25; 
const name: string = "Alice";
Enter fullscreen mode Exit fullscreen mode

4. Type Annotations:
TypeScript supports static typing, where you can annotate variables, function parameters, and return values with types.

let isValid: boolean = true; 
function add(a: number, b: number): number { 
   return a + b; 
}
Enter fullscreen mode Exit fullscreen mode

5. Interfaces and classes:
TypeScript allows the definition of interfaces and classes, promoting object-oriented programming.

interface Person { 
   name: string; 
   age: number; 
} 

class Employee implements Person { 
   constructor(public name: string, public age: number) {} 
}
Enter fullscreen mode Exit fullscreen mode

6. Modules and Imports:
TypeScript supports modular code with import and export statements.

import { Employee } from './Employee'; 
export class Manager extends Employee {}
Enter fullscreen mode Exit fullscreen mode

If you're interested in learning TypeScript, check out my Medium publication for a TypeScript step-by-step tutorial: https://medium.com/@CodingAdventureWithEmma

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay