Skip to content

Interfaces and Type Aliases

Video Lecture

Interfaces and Type Declarations Interfaces and Type Declarations

Description

In TypeScript, Interfaces and Type aliases offer almost the same functionality, in this video we explore that idea.

An Interface/Type is a structure used for type-checking

An Interface/Type defines the properties and types an object can have.

The difference between an Interface and Type alias is the syntax, and that Type aliases don't support declaration merging. Your IDE intellisence may also show it differently.

Start Script

1
2
3
4
5
6
7
const foo = (bar: string) => {
  return 'Hello, ' + bar
}

const baz = 'ABC'

console.log(foo(baz))

Final Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
interface Quux {
  quuz: string
  corge: number
}

// type Quux = {
//   quuz: string
//   corge: number
// }

const foo = (bar: Quux) => {
  return 'Hello, ' + bar.quuz + ' ' + bar.corge
}

let baz = { quuz: 'ABC', corge: 123 }

console.log(foo(baz))

Try it,

tsc foo.ts
node foo.js