DEV Community


Why is the Keyword `as` Needed When Narrowing TypeScript Interfaces With an `if`?

Manav Misra
I'm a JS Subject Matter Expert (SME) that has spent the past few years spearheading curricula and teaching initiatives at colleges and bootcamps, in person and virtually.
・1 min read

Given some interfaces for Circle and Square as follows:

export default interface Circle {
  radius: number
export default interface Square {
  sideLength: number;

I can use as to calculate the area properly:

function getArea(shape: Circle | Square) {
  if ((shape as Circle).radius) {
    return Math.PI * (shape as

Discussion (0)