Understanding Typescript Intersection Types

Scenario

We have two interfaces. Interface A:

interface A { a: string }

And interface B:

interface B { b: number }

And we have a variable that has to contain the intersection of both interface types and we want to declare this.

Approach

var ab: A & B = { a: 'Hola!', b: 1 };

Note that because ab is of type A & B this will give a design time error:

var a: A & B = { a: "hola!" };

We are missing b .

Demo

Brought to You By

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation