Interfaces
- An interface defines the syntax that any entity must adhere to.
- Interfaces define properties, methods, and events, which are the members of the interface.
- Interfaces contain only the declaration of the members.
- It is the responsibility of the deriving class to define the members.
- It often helps in providing a standard structure that the deriving classes would follow.
- Syntax
interface interface_name { }
interface IPerson {
firstName:string,
lastName:string,
sayHi: ()=>string
}
var customer:IPerson = {
firstName:"Tom",
lastName:"Hanks",
sayHi: ():string =>{return "Hi there"}
}
Classes
- A class in terms of OOP is a blueprint for creating objects.
- A class encapsulates data for the object.
- Typescript gives built in support for this concept called class.
- To create an instance of the class, use the new keyword followed by the class name.
Objects
- An object is an instance which contains set of key value pairs.
- The values can be scalar values or functions or even array of other objects.
Namespaces
- A namespace is a way to logically group related code.
- The classes or interfaces which should be accessed outside the namespace should be marked with keyword export.
- To access the class or interface in another namespace, the syntax will be namespaceName.className
Modules
- A module is designed with the idea to organize code written in TypeScript.
- Internal modules came in earlier version of Typescript.
- This was used to logically group classes, interfaces, functions into one unit and can be exported in another module.
- This logical grouping is named namespace in latest version of TypeScript.