Typescript Keywords | Typescript Keywords List

Do you want to know about Typescript keywords? Check out this complete tutorial, where I have explained everything about Typescript keywords and the Typescript keywords list.

TypeScript keywords are reserved words with special syntactic roles, such as defining variables, types, and structures. TypeScript incorporates JavaScript’s keywords and adds new ones, like interface and enum, to support its robust type system. These keywords, including let, const, type, and class, are fundamental in creating well-structured and type-safe TypeScript applications.

What are TypeScript Keywords?

Keywords in TypeScript are reserved words that have special meaning in the language. They cannot be used as identifiers (like variable names, function names, etc.) because they serve specific syntactic and control purposes. TypeScript inherits most keywords from JavaScript and introduces additional ones to support its type system.

Typescript Keywords List

Here’s a table showcasing a list of key TypeScript keywords, along with brief descriptions, syntax, and examples:

KeywordDescriptionSyntaxExample
letDeclares a block-scoped variable.let variableName: type;let age: number = 25;
constDeclares a block-scoped constant.const constantName: type;const PI: number = 3.14;
varDeclares a function-scoped or globally-scoped variable.var variableName: type;var name: string = "John";
functionDefines a function.function name(args): returnType { }function greet(name: string): string { return 'Hello ' + name; }
classDefines a class.class ClassName { }class Person { name: string; }
interfaceDeclares a custom type.interface InterfaceName { }interface IUser { name: string; }
typeDeclares a type alias.type TypeName = Type;type Point = { x: number; y: number; };
enumDeclares an enumeration.enum EnumName { }enum Color { Red, Green, Blue }
publicSpecifies public member access.public memberName: type;public name: string;
privateSpecifies private member access.private memberName: type;private age: number;
protectedSpecifies protected member access.protected memberName: type;protected type: string;
asyncMarks a function as asynchronous.async function name() { }async function fetchData() { }
awaitPauses async function execution until promise settles.await expression;let data = await fetch(url);
nullRepresents the absence of any value.variableName: null;let empty: null = null;
undefinedIndicates uninitialized state of a variable.variableName: undefined;let notAssigned: undefined;
anyAllows any type, bypassing the compiler checks.variableName: any;let flexible: any = 5;
unknownType-safe counterpart of any.variableName: unknown;let mysterious: unknown;
neverRepresents values that never occur.functionName(): never { }function error(): never { throw new Error(); }

This table covers some of the most commonly used TypeScript keywords, providing a quick reference to their purpose, syntax, and application in code.

See also  async Keyword in Typescript

Core TypeScript Keywords

Let’s explore some of the essential TypeScript keywords with examples for a clearer understanding.

  1. let and const

let: Used for variable declaration. Unlike var, let has block scope.

let name = "John";
if (true) {
    let name = "Jane"; // This is a different 'name'
}

const: Declares a constant value. Once assigned, its value cannot be changed.

const PI = 3.14;
  1. interface

interface: Defines the shape of an object. It’s a powerful way to define custom types.

interface User {
    name: string;
    age: number;
}
  1. type

type: Similar to interface, used for aliasing a type or creating new type combinations.

type Point = {
    x: number;
    y: number;
};
  1. class

class: Defines a class in TypeScript.

class Person {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
}
  1. public, private, and protected
  • public: Public member accessible from anywhere.
  • private: Private member, accessible only within the class.
  • protected: Protected member, accessible within the class and its subclasses.
class Animal {
    public name: string;
    private age: number;
    protected type: string;
}
  1. function

function: Declares a function.

function greet(name: string): string {
    return "Hello " + name;
}
  1. enum

enum: Defines a set of named constants.

enum Direction {
    Up,
    Down,
    Left,
    Right,
}
  1. null and undefined

null and undefined are both used to represent the absence of a value, with subtle differences.

let empty: null = null;
let notAssigned: undefined = undefined;
  1. any, unknown, never
  • any: A type representing any JavaScript value. It bypasses the compiler’s type checking.
  • unknown: Similar to any, but safer as it requires type checking before operations.
  • never: Represents values that never occur.
let flexible: any = 5;
let mysterious: unknown;
function throwError(message: string): never {
    throw new Error(message);
}
  1. async and await

async and await keywords are used for asynchronous programming.

async function fetchData() {
    let data = await fetch('https://api.example.com');
    return data.json();
}
Typescript Keywords

Conclusion

I hope you have an idea about the Typescript keywords. I have also shown you the list of useful Typescript keywords.

See also  void Keyword in Typescript

You may also like:

>