Interface and Type
Common Syntax
In TypeScript, interfaces define the structure of objects, specifying the names and types of properties or methods that an object must have. The common syntax for defining an interface in TypeScript is as follows:
Similarly for type definition:
interface InterfaceName
or type TypeName
: Defines the name of the interface.
property1
: Type1
: Specifies the properties of the interface along with their corresponding types. Multiple properties can be defined, each separated by a semicolon.
method1(arg1: ArgType1, arg2: ArgType2): ReturnType;
: Specifies the methods of the interface. Methods are defined with their names, followed by a parameter list in parentheses and the return type. Multiple methods can be defined, each separated by a semicolon.
Example interface:
Example of type:
In TypeScript, types are used to define the shape of data and enforce type checking. There are several common syntaxes for defining types in TypeScript, depending on the specific use case. Here are some examples: