Interfaces

Interface in TypeScript is similar to type aliases but It is used to define the shape of an object and the properties and methods it must have. In TypeScript, interface cannot create custom primitives types, arrays, tuples and union types like type aliases.

Syntax:

interface InterfaceName { 
// key value pair field
}

Where:

  • interface is the keyword used for declaration of interface
  • InterfaceName is the name of interface

Example: This example shows the us of interfaces in typescript.

Javascript




interface User {
    name: string,
    age: number,
}
  
const person1: User = {
    name: 'harry',
    age: 31,
}
  
console.log(person1);
  
interface UserDetails {
    (name: string, age: number): void;
}
  
function displayInfo(
    name: string, age: number): void {
    console.log(name);
    console.log(age);
}
let display: UserDetails = displayInfo;
display('Jim', 35)


Output:

Interface example output

TypeScript Differences Between Type Aliases and Interfaces Type

TypeScript is an open-source programming language that adds static typing and other valuable features to JavaScript. TypeScript is also known as a super-set of JavaScript. Type Aliases and Interfaces Type are used to create custom types in TypeScript. In this article, we will learn about the Differences Between TypeScript Type Aliases and TypeScript Interfaces type.

Similar Reads

Type Aliases

Type Aliases in TypeScript allow you to create a new custom type name that can be reused throughout the code in other words a name for any type. Type Aliases are used to create custom names for primitives, arrays, unions, tuples, objects, and function signatures....

Interfaces

...

Differences Between TypeScript Type Aliases and TypeScript Interfaces Type:

Interface in TypeScript is similar to type aliases but It is used to define the shape of an object and the properties and methods it must have. In TypeScript, interface cannot create custom primitives types, arrays, tuples and union types like type aliases....

Contact Us