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:
Keyword | Description | Syntax | Example |
---|---|---|---|
let | Declares a block-scoped variable. | let variableName: type; | let age: number = 25; |
const | Declares a block-scoped constant. | const constantName: type; | const PI: number = 3.14; |
var | Declares a function-scoped or globally-scoped variable. | var variableName: type; | var name: string = "John"; |
function | Defines a function. | function name(args): returnType { } | function greet(name: string): string { return 'Hello ' + name; } |
class | Defines a class. | class ClassName { } | class Person { name: string; } |
interface | Declares a custom type. | interface InterfaceName { } | interface IUser { name: string; } |
type | Declares a type alias. | type TypeName = Type; | type Point = { x: number; y: number; }; |
enum | Declares an enumeration. | enum EnumName { } | enum Color { Red, Green, Blue } |
public | Specifies public member access. | public memberName: type; | public name: string; |
private | Specifies private member access. | private memberName: type; | private age: number; |
protected | Specifies protected member access. | protected memberName: type; | protected type: string; |
async | Marks a function as asynchronous. | async function name() { } | async function fetchData() { } |
await | Pauses async function execution until promise settles. | await expression; | let data = await fetch(url); |
null | Represents the absence of any value. | variableName: null; | let empty: null = null; |
undefined | Indicates uninitialized state of a variable. | variableName: undefined; | let notAssigned: undefined; |
any | Allows any type, bypassing the compiler checks. | variableName: any; | let flexible: any = 5; |
unknown | Type-safe counterpart of any . | variableName: unknown; | let mysterious: unknown; |
never | Represents 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.
Core TypeScript Keywords
Let’s explore some of the essential TypeScript keywords with examples for a clearer understanding.
- 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;
- interface
interface: Defines the shape of an object. It’s a powerful way to define custom types.
interface User {
name: string;
age: number;
}
- type
type: Similar to interface, used for aliasing a type or creating new type combinations.
type Point = {
x: number;
y: number;
};
- class
class: Defines a class in TypeScript.
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
}
- 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;
}
- function
function: Declares a function.
function greet(name: string): string {
return "Hello " + name;
}
- enum
enum: Defines a set of named constants.
enum Direction {
Up,
Down,
Left,
Right,
}
- 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;
- 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);
}
- 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();
}
Conclusion
I hope you have an idea about the Typescript keywords. I have also shown you the list of useful Typescript keywords.
You may also like:
- TypeScript Hello World Tutorial
- as Keyword in Typescript
- this Keyword in Typescript
- type keyword in Typescript
- override Keyword in Typescript
- unique Keyword in TypeScript
- is Keyword in Typescript
- readonly Keyword in Typescript
- extend Keyword in Typescript
- satisfies Keyword in Typescript
- declare Keyword in Typescript
- const Keyword in Typescript
- super Keyword in Typescript
- require Keyword in Typescript
- arguments keyword in Typescript
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com